Home >Web Front-end >JS Tutorial >Does nodejs support import?
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.
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 globallynpm install babel-cli -gInstall babel-preset-env
npm install babel-preset-env -DThen 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!