[Front-end] Cannot use import statement outside a module Single JS/Node.js solution

Problem Description

When writing a single JS file test, running the code reports "Cannot use import statement outside a module"


Cause Analysis:

Module is loaded using es6 syntax. You need to rewrite/create the package.json file and specify the type as module.


solution:

1. Put the JS file in a folder, and then open the folder with VSCODE.

2. If there is package.json
, add a line of "type": "module" in it .

{
    
    
  "type": "module"
}

3. If there is no package.json , execute npm init -y in the terminal

npm init -y

A package.json file will be automatically generated in the folder, just add a line "type": "module" in it.

{
    
    
  "dependencies": {
    
    
    "@humanwhocodes/env": "^2.2.2"
  },
  "type": "module",   // 加这行就行,其它都是自动生成的。
  "name": "test",
  "version": "1.0.0",
  "main": "test.js",
  "devDependencies": {
    
    },
  "scripts": {
    
    
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

Guess you like

Origin blog.csdn.net/anjue1997/article/details/128659302