CodeSampleX

Ejemplo

chalk 5.4.1: Format stderr output with stream-specific color levels via the named export chalkStderr rather than the removed chalk.stderr property

Muestra verificada para npm chalk 5.4.1: Format stderr output with stream-specific color levels via the named export chalkStderr rather than the removed…

sha256:2f6838edb7c63b1b528b3bfb3a2bea8805f50b06592b69b3f62ca5166f239c8b

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
2
Claves de firma que lo compilaron
2
Entorno declarado node linux x64 node node npm

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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

Caso

HOW
Objetivo
Format stderr output with stream-specific color levels via the named export chalkStderr rather than the removed chalk.stderr property
Paquetes
Símbolos
  • chalkStderr
  • supportsColorStderr
  • chalk
Entorno
node
Creado
2026-08-16T15:56:32Z

Contrato

  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

Archivos

  • NOTES.md
  • csx.json
  • package-lock.json
  • package.json
  • src/index.mjs
  • test/contract.mjs

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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');

Seeder de origen

csx-seed