Skip to content

require.resolve cannot find module after calling require.resolve and installing dependencies  #55169

@lvqq

Description

@lvqq

Version

v20.17.0

Platform

Darwin P03694NWF5 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct  9 21:32:11 PDT 2023; root:xnu-10002.41.9~7/RELEASE_ARM64_T6030 arm64

Subsystem

require

What steps will reproduce the bug?

  1. Init an emty project
npm init
  1. Add a package. e.g. lodash
npm install lodash
  1. Execute this script, the pkg can be any dependencies that are not installed in the global environment and current project. e.g. rollup
const { exec } = require('child_process');
const pkg = 'rollup';

try {
  console.log(require.resolve(pkg));
} catch (e) {
  console.log(e);
}

const child = exec(
  `npm install ${pkg}`,
  () => {
    console.log(require.resolve(pkg));
  }
);

// sync log from child_process to main process
child.stdout?.on('data', (data) => {
  process.stdout.write(data);
});

child.stderr?.on('data', (data) => {
  process.stderr.write(data);
});

The output shows twice Error: Cannot find module 'rollup', the first one is expected; But the second one seems to be a bug.

How often does it reproduce? Is there a required condition?

Emerge necessarily.

The required condition is the first require.resolve call. Without it, the script works fine.

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

Output the resolve path correctly after installing the package

What do you see instead?

Error: Cannot find module 'rollup'
Require stack:
- /Users/Documents/test/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1225:15)
    at Function.resolve (node:internal/modules/helpers:190:19)
    at Object.<anonymous> (/Users/Documents/test/index.js:5:23)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Module._load (node:internal/modules/cjs/loader:1104:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/Documents/test/index.js' ]
}

added 3 packages, and audited 5 packages in 1s

found 0 vulnerabilities
node:internal/modules/cjs/loader:1228
  throw err;
  ^

Error: Cannot find module 'rollup'
Require stack:
- /Users/Documents/test/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1225:15)
    at Function.resolve (node:internal/modules/helpers:190:19)
    at /Users/Documents/test/index.js:13:25
    at ChildProcess.exithandler (node:child_process:414:7)
    at ChildProcess.emit (node:events:519:28)
    at maybeClose (node:internal/child_process:1105:16)
    at ChildProcess._handle.onexit (node:internal/child_process:305:5) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/Documents/test/index.js' ]
}

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    loadersIssues and PRs related to ES module loadersv20.xIssues that can be reproduced on v20.x or PRs targeting the v20.x-staging branch.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions