Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ndcunningham opened this issue Oct 23, 2023 · 0 comments

Comments

@ndcunningham
Copy link

ndcunningham commented Oct 23, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant