server>npm start
[email protected] start
node server.js
(node:12504) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use node --trace-warnings ...
to show where the warning was created)
D:TerProjectserverserver.js:1
import express from ‘express’
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1026:15)
at Module._compile (node:internal/modules/cjs/loader:1061:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
Node.js v17.3.0
2
Answers
To fix this, go to the
package.json
file and add"type": "module"
.This error mainly occurs when you use the import keyword to import a module in Node.js. Or when you omit the
type="module"
attribute in ascript
tag.For Reference : "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6
Add
"type": "module"
to yourpackage.json
file.