Skip to content

[BUG] require.resolve changed behavior in node@12, is broken #27794

Closed
@RomainMuller

Description

@RomainMuller
  • Version: v12.1.0
  • Platform: Darwin f01898256a91 18.6.0 Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64
  • Subsystem: require.resolve

Description

I use require.resolve(request, { paths }) to find a file to load relative to a different directory, however this stopped working in node@12, where require.resolve throws Error: Cannot find module ....

Expected Behavior

The require.resolve call returns the absolute path to the requested file, according to the submitted path(s).

Actual Behavior

In node@12, an error is raised.

Steps to reproduce:

/// index.js
const path = require('path');
try {
  const filePath = require.resolve('./target', {
    paths: [path.resolve(__dirname, 'directory')]
  });
  console.log(`Found: ${filePath}`);
} catch (e) {
  console.error('Failed!');
}

/// directory/target.js
console.log('Reached there!');

Result

$ node -v
v12.1.0
$ node index.js
Failed!

$ /usr/local/opt/node@10/bin/node -v
v10.15.3
$ /usr/local/opt/node@10/bin/node index.js
Found: /tmp/repro/directory/target.js

$ /usr/local/opt/node@8/bin/node -v
v8.16.0
$ /usr/local/opt/node@8/bin/node index.js
Found: /tmp/repro/directory/target.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions