Home  >  Article  >  Web Front-end  >  Does nodejs support import?

Does nodejs support import?

青灯夜游
青灯夜游Original
2021-11-05 16:13:186923browse

Nodejs does not support the import statement. The reason: nodejs adopts the modularization specification of CommonJS and uses the require statement to introduce modules; and import is the modularization specification keyword of ES6.

Does nodejs support import?

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.

nodejs does not support the import statement.

Reason: nodejs adopts the modular specification of CommonJS, using require to introduce modules; and import is the modular specification keyword of ES6.

Sometimes you need to use import, what should you do?

If you want to use import, you must introduce babel escape support and compile it through babel to turn it into the modular code of node.

How to use import/export in node:

If you want to use import, you must introduce babel escape support and compile it through babel to turn it into node modular code.

We can use the

babel-node command in the command line to compile. The babel-node command is not installed independently. In Babel 7 Before the .X version, you need to install the babel-cli

package; in the Babel 7.X version, you need to install the @babel/core and @babel/cli packages.

Note: Here we take the version before 7.

Install babel-cli globally

npm install babel-cli -g
Install babel-preset-env
npm install babel-preset-env -D

Then it turned out to be node server.js, so call it like this:

babel-node --presets env server.js

It should be noted that if it is just for the babel-node command, installing babel-cli will load and install many resources and modules. It is not recommended for use in a production environment due to performance considerations. When you are developing and debugging, you can tinker with it

[Recommended learning: "nodejs tutorial

"]

The above is the detailed content of Does nodejs support import?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn