标签: chalk

chalk - 错误 [ERR_REQUIRE_ESM]:ES 模块的 require()

您好,尝试在我非常简单的应用程序上安装 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)

node.js chalk

107
推荐指数
4
解决办法
10万
查看次数

如何修复“ERR_REQUIRE_ESM”错误?

我正在尝试使用 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)

node.js npm chalk

12
推荐指数
3
解决办法
6万
查看次数

错误:必须使用 import 来加载 ES 模块 - 使用 Typescript 和 Node

背景

尝试改进我的错误编码可见性和结构,并决定添加依赖项 ''' 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)

javascript node.js node-modules typescript chalk

10
推荐指数
1
解决办法
9417
查看次数

节点:进程和进程有什么区别?

当我导入时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。我现在该如何解决这个问题?

javascript node.js chalk webpack

8
推荐指数
2
解决办法
6489
查看次数

为repl提示添加颜色(节点)

我发现在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)

有关如何修复的任何想法?

terminal console node.js read-eval-print-loop chalk

5
推荐指数
1
解决办法
1327
查看次数

在通过子进程传递标准输出时如何应用颜色?

我使用粉笔为命令行节点应用程序的输出着色。通常是这样的:

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)

从子进程中输入标准输出时,如何使用粉笔为颜色添加颜色?

child-process node.js chalk

5
推荐指数
0
解决办法
442
查看次数

在运行时确定终端/ TTY背景颜色

使用粉笔库对终端进行样式化/着色.

import chalk from 'chalk';
Run Code Online (Sandbox Code Playgroud)

如果我使用:

console.log(chalk.blue('foobar'));
Run Code Online (Sandbox Code Playgroud)

这在具有浅色背景的终端中完全可读,但在具有深色背景的终端中完全不可读.

有没有办法在运行时确定终端的背景颜色?


给出的例子:

"npm notice"日志级别为这个问题的情况:

在此输入图像描述

在黑色上阅读蓝色很难.

terminal tty node.js chalk

5
推荐指数
1
解决办法
310
查看次数

如何获取Chalk支持的所有颜色的名称?

我在 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)

types type-assertion typescript chalk

5
推荐指数
1
解决办法
652
查看次数

在 console.log() 中替换和着色参数

有一个很好的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)

它将使用字符串替换并输出正确着色的红色和洋红色:

在此处输入图片说明

现在我想要做的是让函数接受带有替换文字的文本作为第一个参数和可变数量的参数,然后应该:

  1. substitude 对应的替换文字(就像常规的console.log()一样);
  2. 每个传递的参数都应使用红色着色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))但它似乎没有产生我想要的。

javascript node.js chalk

2
推荐指数
1
解决办法
250
查看次数

NodeJS 模块、Chalk 的链接语法如何工作?

我不明白 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同时成为属性和函数?

任何帮助将不胜感激。无论是完整的解释还是朝着正确的方向推动。

javascript syntax chaining node.js chalk

2
推荐指数
1
解决办法
356
查看次数

npm 包 chalk 不适用于 Cypress

我想使用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 但不是蓝色

我究竟做错了什么?

javascript chalk cypress cypress-task

0
推荐指数
1
解决办法
207
查看次数