サンプル
chalk 5.4.1: Format stderr output with stream-specific color levels via the named export chalkStderr rather than the removed chalk.stderr property
検証済みサンプル — npm chalk 5.4.1: Format stderr output with stream-specific color levels via the named export chalkStderr rather than the removed chalk.stderr…
sha256:2f6838edb7c63b1b528b3bfb3a2bea8805f50b06592b69b3f62ca5166f239c8b
このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。
合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。
MIT-0
実行証拠
宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。
- 証拠の基準
- 署名済みコントラクト合格
- 検証レシート
- 2
- ビルドした署名鍵
- 2
宣言された環境
node linux x64 node node npm
検証実行環境
| 環境 | コントラクト | ステージ | 実行日 |
|---|---|---|---|
| node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-16 |
| node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-18 |
ケース
HOW- ゴール
- Format stderr output with stream-specific color levels via the named export chalkStderr rather than the removed chalk.stderr property
- パッケージ
- シンボル
-
- chalkStderr
- supportsColorStderr
- chalk
- 環境
- node
- 作成日
- 2026-08-16T15:56:32Z
コントラクト
- chalk.stderr is undefined on the default export, and chalkStderr maintains its own independent color level unaffected by mutations to chalk.level
- accessing methods on chalk.stderr throws a TypeError because stderr styling migrated to the named export chalkStderr
- chalkStderr formats strings with ANSI escape sequences when enabled even if chalk.level is set to 0
- mutating chalkStderr.level modifies stderr output styling without affecting stdout chalk formatting
- supportsColorStderr is exported separately as a named export and is not attached to default chalk or chalkStderr
ファイル
- NOTES.md
- csx.json
- package-lock.json
- package.json
- src/index.mjs
- test/contract.mjs
ソース
# Notes: Chalk v5 Stderr Stream Formatting and `chalkStderr` Isolation
### What `search_known_solution` answered
- `search_known_solution` returned existing sample `sha256:525f14e9ff41964b2cf8700329f042389c75335c2b13dc4d12796c1cbb8e0f28` ("Configure runtime color formatting and disable ANSI codes in Chalk v5 via chalk.level instead of deprecated chalk.enabled or missing chalk.supportsColor").
- This sample proves a distinct runtime trap: formatting stderr streams independently in Chalk v5 using the named export `chalkStderr` and `supportsColorStderr`, rather than accessing the removed `chalk.stderr` property or assuming shared color state across standard streams.
### What a model would have written instead
- A model would write `chalk.stderr.red('Error message')` expecting the Chalk v4 `chalk.stderr` property to exist, or assume that changing `chalk.level = 0` automatically suppresses ANSI escape sequences across all stream formatters including stderr.
### How the wrong version fails
- Accessing `chalk.stderr.red` fails loudly at runtime with `TypeError: Cannot read properties of undefined (reading 'red')`, while assuming shared level state fails silently when piped stdout (`chalk.level = 0`) leaves uncolored stderr logs or vice versa.
{"case":{"believed":"chalk.stderr provides a stream-targeted instance on the default export and inherits color level changes made to chalk.","caseId":"case:sha256:6f712ad87942dda14996a1ed89c970577664e30e3616917079f4d5e9c6de5db3","contract":["chalk.stderr is undefined on the default export, and chalkStderr maintains its own independent color level unaffected by mutations to chalk.level","accessing methods on chalk.stderr throws a TypeError because stderr styling migrated to the named export chalkStderr","chalkStderr formats strings with ANSI escape sequences when enabled even if chalk.level is set to 0","mutating chalkStderr.level modifies stderr output styling without affecting stdout chalk formatting","supportsColorStderr is exported separately as a named export and is not attached to default chalk or chalkStderr"],"goal":"Format stderr output with stream-specific color levels via the named export chalkStderr rather than the removed chalk.stderr property","kind":"HOW","packages":["pkg:npm/chalk@5.4.1"],"schemaVersion":1,"symbols":["chalkStderr","supportsColorStderr","chalk"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/chalk@5.4.1"],"schemaVersion":1,"symbols":["chalkStderr","supportsColorStderr","chalk"],"verifierAdapter":"node-typescript@1"}
{
"name": "sample-chalk-stderr-isolation",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-chalk-stderr-isolation",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"chalk": "5.4.1"
}
},
"node_modules/chalk": {
"version": "5.4.1",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.4.1.tgz",
"integrity": "sha512-zgVZuo2WcZgfUEmsn6eO3kINexW8RAE4maiQ8QNs8CtpPCSyMiYsULR3HQYkm3w8FIA3SberyMJMSldGsW+U3w==",
"license": "MIT",
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
}
}
}
{
"name": "sample-chalk-stderr-isolation",
"version": "1.0.0",
"private": true,
"type": "module",
"license": "MIT-0",
"dependencies": {
"chalk": "5.4.1"
}
}
import chalk, { chalkStderr, supportsColorStderr } from 'chalk';
/**
* Format a standard output message using the stdout chalk instance.
* @param {string} msg
* @returns {string}
*/
export function formatStdout(msg) {
return chalk.green(msg);
}
/**
* Format a standard error message using the independent chalkStderr instance.
* In Chalk v5, `chalk.stderr` is undefined and `chalkStderr` must be used directly.
* @param {string} msg
* @returns {string}
*/
export function formatStderr(msg) {
return chalkStderr.red.bold(msg);
}
/**
* Set the color level for stdout independently of stderr.
* @param {number} level
*/
export function setStdoutLevel(level) {
chalk.level = level;
}
/**
* Set the color level for stderr independently of stdout.
* @param {number} level
*/
export function setStderrLevel(level) {
chalkStderr.level = level;
}
/**
* Get current color support descriptors for standard error stream.
* @returns {typeof supportsColorStderr}
*/
export function getStderrColorSupport() {
return supportsColorStderr;
}
import { strict as assert } from 'node:assert';
import chalk, { chalkStderr, supportsColorStderr } from 'chalk';
import {
formatStdout,
formatStderr,
setStdoutLevel,
setStderrLevel,
getStderrColorSupport,
} from '../src/index.mjs';
// 1. chalk.stderr was removed from the default export in Chalk v5.
// In Chalk v4, chalk.stderr was attached to the default export.
// In Chalk v5, chalk.stderr is undefined, and accessing methods throws TypeError.
assert.strictEqual(
chalk.stderr,
undefined,
'chalk.stderr is undefined on default export in Chalk v5'
);
assert.throws(
() => chalk.stderr.red('error'),
{
name: 'TypeError',
},
'Accessing methods on chalk.stderr throws TypeError because the property was removed'
);
// 2. chalkStderr is a separate instance whose color level is completely independent of chalk.level.
// Disabling stdout color via chalk.level = 0 does NOT disable stderr color formatting via chalkStderr.
setStdoutLevel(0);
setStderrLevel(1);
assert.strictEqual(chalk.level, 0, 'stdout chalk level should be 0');
assert.strictEqual(chalkStderr.level, 1, 'stderr chalkStderr level should be 1');
const stdoutResult = formatStdout('job complete');
const stderrResult = formatStderr('fatal error');
// stdout formatting produces plain text because chalk.level = 0
assert.strictEqual(stdoutResult, 'job complete', 'stdout formatting emits plain text when level is 0');
assert.ok(!stdoutResult.includes('\u001b['), 'stdout output must not contain ANSI escape codes');
// stderr formatting produces ANSI styled text because chalkStderr.level = 1
assert.strictEqual(
stderrResult,
'\u001b[31m\u001b[1mfatal error\u001b[22m\u001b[39m',
'chalkStderr emits ANSI codes when its independent level is enabled'
);
assert.ok(stderrResult.includes('\u001b[31m'), 'stderr output contains red ANSI escape code');
// 3. Mutating chalkStderr.level modifies stderr output styling without affecting stdout formatting
setStdoutLevel(1);
setStderrLevel(0);
assert.strictEqual(chalk.level, 1, 'stdout chalk level restored to 1');
assert.strictEqual(chalkStderr.level, 0, 'stderr chalkStderr level disabled to 0');
assert.strictEqual(
formatStdout('job complete'),
'\u001b[32mjob complete\u001b[39m',
'stdout emits green ANSI escape code when chalk.level is 1'
);
assert.strictEqual(
formatStderr('fatal error'),
'fatal error',
'stderr emits plain text when chalkStderr.level is 0'
);
// 4. supportsColorStderr is exported separately and is not attached to default chalk or chalkStderr
assert.strictEqual(chalk.supportsColorStderr, undefined);
assert.strictEqual(chalkStderr.supportsColorStderr, undefined);
assert.strictEqual(chalkStderr.supportsColor, undefined);
assert.strictEqual(getStderrColorSupport(), supportsColorStderr);
console.log('CONTRACT PASS: chalkStderr provides independent stderr formatting in Chalk v5');