报错信息
PS D:\myCode\excercise\node> node index.js
(node:26820) 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:\myCode\excercise\node\index.js:1
import { mkdir } from 'node:fs/promises';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1176:20)
at Module._compile (node:internal/modules/cjs/loader:1218:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
Node.js v18.16.0
PS D:\myCode\excercise\node>
node运行js文件
// 当前 node 版本
## node运行js文件
```bash
// 当前 node 版本
D:\work\RD\zl-vue3-plugins\packages\model>node -v
v18.16.0
运行支持 ES Modules 模块
ES 6 模块语法
添加package.json,添加module类型
{
"type":"module"
}
import { mkdir } from 'node:fs/promises';
try {
const projectFolder = new URL('./test/project/', import.meta.url);
const createDir = await mkdir(projectFolder, { recursive: true });
console.log(`created ${createDir}`);
} catch (err) {
console.error(err.message);
}
在终端下使用node运行 index.js 文件
PS D:\myCode\excercise\node> node index.js
created D:\myCode\excercise\node\test
PS D
运行支持 ES Modules 模块
ES 6 模块语法
添加package.json,添加module类型
{
"type":"module"
}
import { mkdir } from 'node:fs/promises';
try {
const projectFolder = new URL('./test/project/', import.meta.url);
const createDir = await mkdir(projectFolder, { recursive: true });
console.log(`created ${createDir}`);
} catch (err) {
console.error(err.message);
}
在终端下使用node运行 index.js 文件
PS D:\myCode\excercise\node> node index.js
created D:\myCode\excercise\node\test
PS D