dividab / tsconfig-paths

Load node modules according to tsconfig paths, in run-time or via API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When using tsconfig-paths require.resolve returns a local file (.json) instead of a npm packaged file

ndcunningham opened this issue · comments

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

  1. Navigate to the project directory and install dependencies: npm install
  2. Run the main.js script: node main.js
    The script is expected to resolve to a .js file within the node_modules/.bin directory. However, it incorrectly resolves to a local nx.json file instead.
    Additionally we have a foo.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.