Node import error solution: 1. Execute "npm install --save babel-corenpm install --save babel-preset-env" in the project root directory; 2. Create a A file named ".babelrc"; 3. Run the project with "npm start" and "babel-node app.js".
The operating environment of this tutorial: Windows 10 system, node18.4.0 version, Dell G3 computer.
What should I do if I get an error when importing node?
Run the nodejs project, npm start starts the project import and reports an error, SyntaxError: Cannot use import statement outside a module
The error when running the nodejs project is as follows:
It probably means that nodejs does not support the import syntax. If you want to support it, you need babel to support it.
So let’s install babel. With babel, you can use more advanced lexical methods!
In the project root directory, execute:
npm install --save babel-corenpm install --save babel-preset-env 或者 npm install --save babel-preset-es2015npm install babel-cli -g
Then create a file named .babelrc
in the project root directory, the content of the file Enter as follows (please note that when creating this kind of file system under the window system, you will be prompted: "You must enter the file name". You can find other ways to create it. I created this file in the project directory of the development tool, and You can use the vim command of
cmder artifact):
{ "presets": [ "es2015" ], "plugins": []}
{ "presets": [ "env" ], "plugins": []}
npm start and
babel-node app.js to run the project.
Recommended learning: "react video tutorial"
The above is the detailed content of What to do if node import error occurs. For more information, please follow other related articles on the PHP Chinese website!