SyntaxError: Cannot use import statement outside a module
详情
(base) kangder@Kangder 18_useExpress % node index.js
(node:17192) 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)
/Users/kangder/web/learn_node/learn_node/18_useExpress/index.js:8
import express from "express";
^^^^^^SyntaxError: Cannot use import statement outside a moduleat Object.compileFunction (node:vm:360:18)at wrapSafe (node:internal/modules/cjs/loader:1094:15)at Module._compile (node:internal/modules/cjs/loader:1129:27)at Object.Module._extensions..js (node:internal/modules/cjs/loader:1219:10)at Module.load (node:internal/modules/cjs/loader:1043:32)at Function.Module._load (node:internal/modules/cjs/loader:878:12)at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)at node:internal/main/run_main_module:22:47
修改方式
- 在package.json文件中配置 type 属性
{"dependencies": {"express": "^4.21.0","fs-then": "^0.2.0"},"type": "module"
}