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:
- How to Solve Nodejs error: cannot find module ‘. /application’
- Error: Cannot find module ‘./application‘ [How to Solve]
- js: SyntaxError: Cannot use import statement outside a module
- [Solved] SyntaxError: Cannot use import statement outside a module
- [Solved] Uncaught SyntaxError: Cannot use import statement outside a module
- Node.js Error: Error: Cannot find module ‘express‘ [How to Solve]
- Vue Project Error: Proxy error: Could not proxy request [How to Solve]
- Node.js Error: Cannot find module express [How to Solve]
- [Solved] Vue Error: Module build failed Error Node Sass version 6.0.1 is incompatible with ^4.0.0.
- [Solved] Syntax Error: Error: Loading PostCSS Plugin failed: Cannot find module ‘autoprefixer‘
- Error: Rule can only have one resource source (provided resource and test + include + exclude)
- [Solved] bug: error Command failed with exit code 1.
- [Solved] Error: ENOENT: no such file or directory, scandir ‘..\node_modules\node-sass\vendor‘
- Module build failed: Error: Couldn’t find preset “es2015” relative to directory
- [Solved] error C:\Users\HP\Desktop\VueProject\vue_cli\node_modules\node-sass: Command failed.Exit code: 1
- [Solved] VUE npm install Error: Module build failed: Error: ENOENT: no such file or directory, scandir
- Echarts Map Error: Error: Invalid geoJson format Cannot read property ‘length‘ of undefined
- [Solved] start value has mixed support, consider using flex-start instead
- Vue-cli2 sub environment packaging
- [Solved] npm Error: Class extends value undefined is not a constructor or null