SyntaxError: Cannot use import statement outside a module
Problem: When debugging js code with vs code, I get “SyntaxError: Cannot use import statement outside a module”
import express from "express";
const app=express();
app.listen(9527,function(){
console.log(9527);
})
Solution:
npm init -y
Add type(“type”:”module”,) in package. json
{
"name": "serve",
"version": "1.0.0",
"description": "Server-side interface building for js phase projects",
"main": "index.js",
"type":"module",
"scripts": {
"node":"node app.js",
"dev":"nodemon ./app.js"
},
"keywords": [
"shop"
],
"author": "wff",
"license": "ISC",
"devDependencies": {
"express": "^4.17.3",
"nodemon": "^2.0.15"
}
}
The terminal runs node index.js or vs Code F5 can be run.
Read More:
- [Solved] SyntaxError: Cannot use import statement outside a module
- [Solved] SyntaxError: Cannot use import statement outside a module
- js: SyntaxError: Cannot use import statement outside a module
- [Solved] Uncaught SyntaxError: Cannot use import statement outside a module
- [Solved] JS Error: Uncaught SyntaxError: Illegal return statement
- [Solved] Uncaught SyntaxError: The requested module does not provide an export named
- [Solved] component import error is declared in the statement of components
- [Solved] Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension
- [Solved] Vue Import swiper.css Error: Module not found…
- Vue-router import Dynamic Module Error [How to Solve]
- [Solved] Uniapp project use vant icon Error: (module build failed from./node_modules/postcss loader/SRC/index)
- Vue Import element-plus Error: Failed to resolve import “element-pluslibtheme-chalkindex.css“ from “src
- [Solved] ajax Error: Uncaught SyntaxError: Unexpected end of JSON input
- node.js yarn Error: SyntaxError: Unexpected string [How to Solve]
- [Solved] React Startup Error at the First time :SyntaxError: Unexpected token
- @requestbody: How to Use or Not Use
- [Solved] Webpack compile error: SyntaxError: Unexpected token =
- [Solved] SyntaxError: E:\IdeaProject\src\main.js: Identifier ‘ElementPlus‘ has already been declared
- How to Solve Nodejs error: cannot find module ‘. /application’
- [Solved] Uncaught SyntaxError: Invalid Unicode escape sequence