fix(common): add resolveJsonModule to ts-node compilerOptions (fixes #816)#2189
fix(common): add resolveJsonModule to ts-node compilerOptions (fixes #816)#2189
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. |
…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
96f1f05 to
b0bafff
Compare
Problem
Importing
.jsonfiles in TypeScript webpack configs (e.g.import * as pkg from './package.json') fails with:This happens because ts-node was not configured with
resolveJsonModule: truewhen loading user webpack config files.Root Cause
load-module.tscallsts-node'sregister()with an explicitcompilerOptionsoverride. This override setsmodule: 'CommonJS'andtypes: ['node']but does not includeresolveJsonModule. As a result, even if the user'stsconfig.jsonhasresolveJsonModule: true, it gets overridden tofalse(the default).Fix
Add
resolveJsonModule: trueto the explicitcompilerOptionspassed tots-node.register(). This is a safe, additive change — it only affects the ts-node compilation context used when loading user config files, not the application's own TypeScript compilation.Verification
Confirmed TS2732 error without the fix and clean compile with it using the same
import * as pkg from './package.json'pattern.Adds integration test:
custom-webpack: TS config with JSON importwhich builds the app using a webpack config that importspackage.json.Closes #816