CodeSampleX

샘플

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

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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

컨트랙트

  1. chalk.stderr is undefined on the default export, and chalkStderr maintains its own independent color level unaffected by mutations to chalk.level
  2. accessing methods on chalk.stderr throws a TypeError because stderr styling migrated to the named export chalkStderr
  3. chalkStderr formats strings with ANSI escape sequences when enabled even if chalk.level is set to 0
  4. mutating chalkStderr.level modifies stderr output styling without affecting stdout chalk formatting
  5. 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

소스 아티팩트 내려받기 (tar.gz)

소스

NOTES.md
# 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.
csx.json
{"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"}
package-lock.json
{
  "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"
      }
    }
  }
}
package.json
{
  "name": "sample-chalk-stderr-isolation",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "chalk": "5.4.1"
  }
}
src/index.mjs
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;
}
test/contract.mjs
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');

오리진 시더

csx-seed