Home  >  Article  >  Web Front-end  >  What to do if node import error occurs

What to do if node import error occurs

藏色散人
藏色散人Original
2023-01-03 16:01:362680browse

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".

What to do if node import error occurs

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:

What to do if node import error occurs

It probably means that nodejs does not support the import syntax. If you want to support it, you need babel to support it.

babel installation

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": []}
or:

{                
    "presets": [ 
         "env"   
     ],          
    "plugins": []}
So far, babel has been installed.

Next, you can use

npm start and babel-node app.js to run the project.
Recommended learning: "What to do if node import error occursreact 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!

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