fix(common): add resolveJsonModule to ts-node compilerOptions (fixes #816)#2189
fix(common): add resolveJsonModule to ts-node compilerOptions (fixes #816)#2189just-jeb wants to merge 4 commits into
Conversation
|
Thanks for the PR! After discussion, we have decided not to add
The workaround is straightforward: add Appreciate the contribution! |
|
Correction to my previous comment: looking more carefully at the issue history, the reporter confirmed that adding resolveJsonModule to the user tsconfig does NOT fully solve it (it still fails for the karma builder). So the user-side workaround is incomplete. The actual validated workarounds are:
We are reconsidering the approach. Fixing it at the builder level is valid but would be a deliberate decision for a major version. Apologies for the premature close — we will reopen and track this properly. |
|
Updated analysis after reading the code: The root cause is that There are two possible approaches to fix this properly:
Both approaches could be a breaking change for setups that rely on the current minimal override behavior. We are deferring this to the next major version. Keeping this PR open as a reference implementation for when that time comes. |
96f1f05 to
b0bafff
Compare
8cab3b2 to
85ba473
Compare
|
Option 1 seems fine. |
273d7c2 to
44f1069
Compare
…816) When a TypeScript webpack config imports a JSON file (e.g. `import * as pkg from './package.json'`), ts-node throws TS2732 ('Cannot find module') if the project tsconfig uses moduleResolution:'node' and does not set resolveJsonModule:true. The root cause: the builder's ts-node registration overrides `module: 'CommonJS'` but never sets `resolveJsonModule`. With moduleResolution:'node' (the Angular default before v17), TypeScript requires an explicit `resolveJsonModule: true` to allow JSON imports. Fix: add `resolveJsonModule: true` to the compilerOptions override in `_tsNodeRegister()`. This is safe to always enable — it has no downside and works with all moduleResolution modes (node, node16, bundler, etc.). Reproduction: tsconfig with moduleResolution:node + TS webpack config importing package.json -> TS2732 Fix verifier: same config with resolveJsonModule injected by ts-node -> build succeeds Integration test added: ts-config-json-module-import
… test moduleResolution:node cannot resolve Angular 21 subpath exports, making the ts-config-json-import configuration fundamentally broken.
44f1069 to
27688d8
Compare
…st for JSON import
- Add examples/custom-webpack/sanity-app/extra-webpack.config.ts that imports
package.json (uses `import { name } from './package.json'`). sanity-app's
tsconfig does not set resolveJsonModule, so this requires the fix in #816 to
work: ts-node must be registered with resolveJsonModule:true.
- Add 'ts-json-import' build configuration to sanity-app/angular.json wiring
in the new config.
- Add 'ts-config-json-import' integration test entry to integration.js.
- Remove the ts-node options toHaveBeenCalledWith assertion from
transform-factories.spec.ts (it checked implementation detail, not behavior).
Keep the warning-on-duplicate-tsconfig assertion which tests real behavior.
PR Checklist
PR Type
What is the current behavior?
Importing
.jsonfiles in TypeScript webpack configs (e.g.import * as pkg from './package.json') fails with TS2732 even when the user's tsconfig hasresolveJsonModule: true, because ts-node's explicitcompilerOptionsoverride inload-module.tsdoes not includeresolveJsonModule, which resets it to the defaultfalse.Issue Number: #816
What is the new behavior?
resolveJsonModule: trueis added to the explicitcompilerOptionspassed tots-node.register(), so JSON imports work in TypeScript webpack config files.New integration test:
custom-webpack: TS config with JSON import.Does this PR introduce a breaking change?
resolveJsonModuleis a permissive flag — it enables a behavior that was previously rejected. No existing valid config can be broken by this change.Other information
Note: This PR modifies
load-module.tswhich is also rewritten by PR #1659. If #1659 merges first, this fix needs to be applied toregister-ts-project.tsinstead.