您好,尝试在我非常简单的应用程序上安装 chalk,然后出现错误:
Error [ERR_REQUIRE_ESM]: require() of ES Module my-file-is-here and chalk\node_modules\chalk\source\index.js from my-file-is-here not supported.
Instead change the require of index.js in my-file-is-here to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (`my-file-is-here`) {
code: 'ERR_REQUIRE_ESM'
}
Run Code Online (Sandbox Code Playgroud)
这就是我的代码:
const os = require("os")
const chalk = require("chalk")
console.log("app running")
Run Code Online (Sandbox Code Playgroud)
小智 116
这与您使用的版本有关,我认为是 5.0.0。使用 chalk@4.1.2 代替
npm uninstall chalk然后
npm i chalk@4.1.2现在你可以运行你的代码了
const chalk = require('chalk');
console.log(chalk.blue('Hello world!'));
Run Code Online (Sandbox Code Playgroud)
Her*_*gon 21
chalk^5改为使用4您可以dynamic import与粉笔^5(ESM version)一起使用CJS
const chalk = import("chalk").then(m=>m.default);
// Option 1
async function main(){
console.log((await chalk).gray(">", ...commands));
}
// Option 2
async function main2(){
const _chalk = await chalk;
console.log(_chalk.gray(">", ...commands));
}
Run Code Online (Sandbox Code Playgroud)
降级v4并不是一个长期的解决方案。您应该使用v5为 ESM 做好准备的版本
小智 20
Step-1 npm uninstall chalk(删除所有chalk文件)
步骤2 npm install chalk@4.1.0
const chalk = require("chalk");
console.log(chalk.green("Hello World"));
Run Code Online (Sandbox Code Playgroud)
完毕
| 归档时间: |
|
| 查看次数: |
104179 次 |
| 最近记录: |