问题遇到的现象和发生背景
nodejs的版本是12.17.0 在练习一个自定义cli的过程中,遇到了以下问题:
安装的依赖有如下:
"dependencies": {
"chalk": "^5.0.1",
"clear": "^0.1.0",
"commander": "^9.2.0",
"download-git-repo": "^3.0.2",
"figlet": "^1.5.2",
"handlebars": "^4.7.7",
"open": "^8.4.0",
"ora": "^6.1.0"
}
问题相关
PS E:\node-proj\vue-auto-router-cli> kkb init test
internal/modules/cjs/loader.js:1154
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: E:\node-proj\vue-auto-router-cli\node_modules\chalk\source\index.js
require() of ES modules is not supported.
require() of E:\node-proj\vue-auto-router-cli\node_modules\chalk\source\index.js from E:\node-proj\vue-auto-router-cli\lib\init.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from E:\node-proj\vue-auto-router-cli\node_modules\chalk\package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1154:13)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (E:\node-proj\vue-auto-router-cli\lib\init.js:4:15)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14) {
code: 'ERR_REQUIRE_ESM'
}
PS E:\node-proj\vue-auto-router-cli>
运行结果及报错内容
网上能搜到的方法都用了 统统没用 欢迎有经验的玩家支招