您好,尝试在我非常简单的应用程序上安装 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) 我正在尝试使用 chalk npm。我的代码是:
const chalk = require('chalk');
console.log(
chalk.green('All sytems go') +
chalk.orange('until').underline +
chalk.black(chalk.bgRed('an error occurred'))
);
Run Code Online (Sandbox Code Playgroud)
当我输入 node main.js 时,我在终端中收到此错误
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/ezell/Documents/CodeX/NPM/node_modules/chalk/source/index.js from /Users/ezell/Documents/CodeX/NPM/main.js not supported.
Instead change the require of index.js in /Users/ezell/Documents/CodeX/NPM/main.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/Users/ezell/Documents/CodeX/NPM/main.js:1:15) {
code: 'ERR_REQUIRE_ESM'
}
Run Code Online (Sandbox Code Playgroud) 背景
尝试改进我的错误编码可见性和结构,并决定添加依赖项 ''' chalk ''',这需要 Pure ESM。最初,我的 ts.config/package.json 是一个 common.js 文件,没有将“类型”声明为“模块”。按照以下 chalk 文档和类似的堆栈溢出故障排除进行了一些更改。重新加载我的应用程序后,我收到此错误:
错误
[user]
[user] > user@1.0.0 start
[user] > ts-node-dev src/index.ts
[user]
[user] [INFO] 02:43:13 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.8.0, typescript ver. 4.7.2)
[user] Compilation error in /app/src/index.ts
[user] Error: Must use import to load ES Module: /app/src/index.ts
[user] at Object.<anonymous> (/app/src/index.ts:1:7)
[user] at Module._compile (node:internal/modules/cjs/loader:1105:14)
[user] at Module._compile (/app/node_modules/source-map-support/source-map-support.js:568:25)
[user] at Module.m._compile (/tmp/ts-node-dev-hook-07536074778072654.js:69:33)
[user] at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
[user] at require.extensions..jsx.require.extensions..js (/tmp/ts-node-dev-hook-07536074778072654.js:114:20)
[user] at require.extensions.<computed> (/tmp/ts-node-dev-hook-07536074778072654.js:71:20) …Run Code Online (Sandbox Code Playgroud) 当我导入时node:process它工作正常。但是,当我尝试要求相同时,它会给出错误。
这工作正常:
import process from 'node:process';
Run Code Online (Sandbox Code Playgroud)
但是当我尝试要求相同时,它会抛出错误:
const process = require('node:process');
Run Code Online (Sandbox Code Playgroud)
Error: Cannot find module 'node:process'
我很好奇process在 commonjs 和 module 中都可以使用的 与node:process.
另外,后续,我使用 webpack 来捆绑我的 js,当我尝试运行捆绑的代码并意识到chalk导入node:process和时node:os,我发现了这个错误node:tty。我现在该如何解决这个问题?
我发现在repl中为提示添加颜色确实有助于分离输出.我通过使用NPM的粉笔来实现这一点,但这会在提示和光标之间增加一些空间.
var term = repl.start({
prompt: chalk.blue('goose> '),
eval: function(cmd, context, filename, cb){
...
}
});
Run Code Online (Sandbox Code Playgroud)
提示出现如下('|'是光标):
goose> |
Run Code Online (Sandbox Code Playgroud)
有关如何修复的任何想法?
我使用粉笔为命令行节点应用程序的输出着色。通常是这样的:
console.log(chalk.red(error));
Run Code Online (Sandbox Code Playgroud)
但是,我需要通过管道从子进程输出stdout,并为输出加上颜色:
var child = childProcess.spawn('foo', args);
child.stdout.pipe(process.stdout); //how to use chalk to colour this?
Run Code Online (Sandbox Code Playgroud)
从子进程中输入标准输出时,如何使用粉笔为颜色添加颜色?
使用粉笔库对终端进行样式化/着色.
import chalk from 'chalk';
Run Code Online (Sandbox Code Playgroud)
如果我使用:
console.log(chalk.blue('foobar'));
Run Code Online (Sandbox Code Playgroud)
这在具有浅色背景的终端中完全可读,但在具有深色背景的终端中完全不可读.
有没有办法在运行时确定终端的背景颜色?
给出的例子:
该"npm notice"日志级别为这个问题的情况:
在黑色上阅读蓝色很难.
我在 TypeScript 中使用Chalk NPM 包。如果我动态设置 Chalk 的颜色,则会收到 TS 错误。我可以使用类型断言,chalk[color] as Chalk但如果可能的话,我更愿意使用类型谓词,这需要我能够访问支持的颜色列表。
那么,有没有一种方法可以访问 Chalk 中支持的颜色列表,或者有其他方法来解决这个问题,而不使用类型断言,并且可能使用类型谓词?
可能需要启用instrict中的选项才能显示错误。compilerOptionstsconfig.json
代码如下,错误在注释中:
import chalk from 'chalk';
function getColor(): string {
return 'blue';
}
const color = getColor();
/**
* Element implicitly has an 'any' type because expression of type 'string'
* can't be used to index type 'Chalk & { supportsColor: ColorSupport; }'.
*
* No index signature with a parameter of type 'string' was found on type 'Chalk …Run Code Online (Sandbox Code Playgroud) 有一个很好的Node.js调用模块,chalk它允许将颜色和文本格式应用于console.log()函数。
如果我写这样的东西:
console.log("test string substitution with coloring: %s, %s", chalk.red("red"), chalk.magenta("magenta"));
Run Code Online (Sandbox Code Playgroud)
它将使用字符串替换并输出正确着色的红色和洋红色:
现在我想要做的是让函数接受带有替换文字的文本作为第一个参数和可变数量的参数,然后应该:
console.log()一样);chalk.red();例如:
function log(text, ...args) {
// magic here
}
log("This must be %s, and %s as well", "red", "this must be red");
Run Code Online (Sandbox Code Playgroud)
这将给出:
我试过使用,console.log(text, chalk.red.apply(null, args))但它似乎没有产生我想要的。
我不明白 Chalk 的(NodeJS 模块)语法是如何工作的(这让我很困扰)。我已经广泛寻找答案,但我没有任何运气,并且可能不知道我需要寻找的技术术语。我尝试在 StackOverflow、“方法链接”、“原型”等上查找 chalk 特定的问题。尝试查看 Chalk 的源代码,但似乎仍然无法找出我的答案。有问题的语法是:
// Code snippet from the Chalk NPM Page.
log(chalk.blue.bgRed.bold('Hello world!'));
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
Run Code Online (Sandbox Code Playgroud)
我熟悉方法链并熟悉在对象中存储函数。我的主要问题是:如何chalk.blue同时成为属性和函数?
任何帮助将不胜感激。无论是完整的解释还是朝着正确的方向推动。
我想使用https://www.npmjs.com/package/chalk作为控制台日志。
现在如果我使用console.log它就可以了
console.log(chalk.blue("Hello World"))
但是当我用 cy.task 运行它时,它不会给出任何错误,但不会显示任何着色
cy.task('log', chalk.blue("Hello World"));它打印 Hello World 但不是蓝色
我究竟做错了什么?
chalk ×11
node.js ×9
javascript ×5
terminal ×2
typescript ×2
chaining ×1
console ×1
cypress ×1
cypress-task ×1
node-modules ×1
npm ×1
syntax ×1
tty ×1
types ×1
webpack ×1