fix(common): add resolveJsonModule to ts-node compilerOptions (fixes #816)#2189
Merged
Conversation
Owner
Author
|
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
Owner
Author
|
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.