Skip to content

fix(misc): use default import for chalk in @nx/workspace output.ts#35523

Open
jdgarvey wants to merge 1 commit intonrwl:masterfrom
jdgarvey:fix/workspace-output-chalk-import
Open

fix(misc): use default import for chalk in @nx/workspace output.ts#35523
jdgarvey wants to merge 1 commit intonrwl:masterfrom
jdgarvey:fix/workspace-output-chalk-import

Conversation

@jdgarvey
Copy link
Copy Markdown

Current Behavior

require('@nx/workspace') throws a TypeError in 22.7.x, crashing any generator that imports from @nx/workspace:

TypeError: Cannot read properties of undefined (reading 'inverse')
    at new CLIOutput (.../node_modules/@nx/workspace/src/utils/output.js:14:38)

Fixes #35521

Root Cause

PR #34111 (commit 732a08c) added esModuleInterop: true to tsconfig.base.json. This caused import * as chalk from 'chalk' in output.ts to compile to tslib.__importStar(require("chalk")) instead of require("chalk") directly.

tslib.__importStar only copies own enumerable properties — chalk v4 exposes reset, bold, inverse, etc. as prototype getters on the Chalk class, not own properties. After wrapping, chalk.reset is undefined → crash.

Changes

packages/workspace/src/utils/output.ts — change import * as chalk from 'chalk' to import chalk from 'chalk'.

With esModuleInterop: true, the default import compiles to __importDefault(require("chalk")).default which returns the chalk instance directly with its prototype chain intact.

How Has This Been Tested?

Same fix applied previously for the identical root cause in:

Verified by simulating the compiled output in a minimal repro workspace — see #35521 for the reproduction script.

Checklist

  • nx affected:test has been run
  • My changes do not require a change to the documentation

@jdgarvey jdgarvey requested a review from a team as a code owner April 30, 2026 19:23
@jdgarvey jdgarvey requested a review from AgentEnder April 30, 2026 19:23
@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 30, 2026

👷 Deploy request for nx-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit ad3b642

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 30, 2026

👷 Deploy request for nx-dev pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit ad3b642

tslib's __importStar only copies own enumerable properties; chalk v4
puts reset/bold/etc on the prototype, so chalk.reset becomes undefined
after wrapping — crashing any code that requires @nx/workspace.

esModuleInterop:true was added in nrwl#34111 (commit 732a08c), causing
`import * as chalk` to emit __importStar in compiled output and expose
this latent bug.

fix: change to default import (`import chalk from 'chalk'`) which
compiles to __importDefault().default, preserving chalk's prototype chain.

same root cause fixed previously in:
- nrwl#21201 (@nx/eslint-plugin)
- nrwl#26667 (@nx/angular)

Fixes nrwl#35521
@jdgarvey jdgarvey force-pushed the fix/workspace-output-chalk-import branch from 8b201b0 to ad3b642 Compare April 30, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(@nx/workspace): chalk/tslib __importStar incompatibility crashes require('@nx/workspace')

2 participants