tsconfig-paths
tsconfig-paths copied to clipboard
When using tsconfig-paths require.resolve returns a local file (.json) instead of a npm packaged file
The tsconfig-paths
package is resolving to a local JSON file rather than the expected JavaScript file from a node module binary. This behaviour is causing unexpected issues when attempting to resolve modules in a specific use case.
Steps to Reproduce
Clone the repository: https://github.com/jaysoo/nx-tsconfig-path-issue
- Navigate to the project directory and install dependencies:
npm install
- Run the
main.js
script:node main.js
The script is expected to resolve to a.js
file within thenode_modules/.bin
directory. However, it incorrectly resolves to a localnx.json
file instead. Additionally we have afoo.json
being resolved (To ensure that it was not the nx package which is causing the resolution issue)
Expected Behavior
The require.resolve
function should resolve to the .js
file specified in the node_modules/nx/bin/nx.js
directory
Actual Behavior
The require.resolve
function resolves to a local nx.json
file.
Potential Cause and Solution
The issue seems to stem from the matchFromAbsolutePaths
function in the tsconfig-paths
package, specifically this block of code which uses require.extensions
to include .json
files in the resolution process.
interface RequireExtensions extends Dict<(m: Module, filename: string) => any> {
'.js': (m: Module, filename: string) => any;
'.json': (m: Module, filename: string) => any;
'.node': (m: Module, filename: string) => any;
}
Is there a way to exclude .json
files from this resolution process, or to otherwise prioritize .js
files from node_modules/{package}/bin/...
over local .json
files? This would ensure that the require.resolve
function behaves as expected and resolves to the correct module.