nodejs / nodejs/node

`require.resolve` regression starts from Node v12 with `paths`

Open
#58,487 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

never-stale
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version

v20.19.2

Platform
Darwin MacBookPro 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000 arm64
Subsystem

No response

What steps will reproduce the bug?

https://github.com/JounQin/test/tree/repro/node_require_resolve_paths

Simply run node test.js

On node 10:

`resolve` npm package: <CWD>/fixtures/bar.js
`require.resolve` built-in function: <CWD>/fixtures/bar.js
How often does it reproduce? Is there a required condition?

Persist

What is the expected behavior? Why is that the expected behavior?

There is a related old issue #29139.

What do you see instead?

On node 12+, of course including Node LTS (20+)

`resolve` npm package: /Users/JounQin/Workspaces/GitHub/test/fixtures/bar.js
node:internal/modules/cjs/loader:1404
  throw err;
  ^

Error: Cannot find module 'bar'
Require stack:
- <CWD>/test.js
    at Function._resolveFilename (node:internal/modules/cjs/loader:1401:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at Object.<anonymous> (/Users/JounQin/Workspaces/GitHub/test/test.js:16:11)
    at Module._compile (node:internal/modules/cjs/loader:1730:14)
    at Object..js (node:internal/modules/cjs/loader:1895:10)
    at Module.load (node:internal/modules/cjs/loader:1465:32)
    at Function._load (node:internal/modules/cjs/loader:1282:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '<CWD>/test.js' ]
}
Additional information

I believe there is no reports because most projects use http://npmjs.com/resolve instead, and it behaviors same as Node 10-, and I'm trying to remove resolve as dependency, so I just met this issue at https://github.com/un-ts/eslint-plugin-import-x/pull/272#issuecomment-2915145320

cc @cjihrig

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the linked reproduction's test.js under Node 10 and Node 12+ and compare the require.resolve results when paths is supplied. Read the CommonJS loader path shown in the stack trace and related issue #29139; done means the reproduction resolves fixtures/bar.js consistently without the external resolve package.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.