Why doesn't nodejs support import?
This article mainly explains "Why nodejs does not support import". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn why nodejs does not support import.
Nodejs does not support import statements because nodejs uses the modular specification of CommonJS and introduces modules using the modular statement, while import is the modular specification keyword of ES6.
This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.
Nodejs does not support import statements.
Reason: nodejs uses the modular specification of CommonJS and introduces modules using require, while import is the modular specification keyword of ES6.
And sometimes you need to use import, what should I do?
If you want to use import, you must introduce babel escape support and compile it through babel to make it modular code for node.
The method of using import/export in node:
If you want to use import, you must introduce babel escape support and compile it through babel to make it modular code for node.
We can use it on the command line
Babel-node command to compile, babel-node command is not a separate installation, before the Babel 7.x version, you need to install babel-cli
The package is available; in Babel version 7.x, you need to install @ babel/core and @ babel/cli packages.
Note: this is an example of the version before 7.x. Version 7.x can go to the official website to find the use case of the corresponding package.
Install the necessary plug-ins
Global installation of babel-cli
Npm install babel-cli-g
Install babel-preset-env
Npm install babel-preset-env-D
And then it turned out to be node server.js. Instead, it called: babel-node-- presets env server.js.
It should be noted that if it is only for the babel-node command, installing babel-cli will load and install a lot of resources and modules, and it is not recommended for use in a production environment for performance reasons. When you are developing and debugging, you can play with it.
Thank you for your reading, the above is the content of "Why nodejs does not support import". After the study of this article, I believe you have a deeper understanding of why nodejs does not support import, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!