Skip to content

Node 12: require.resolve(relativePath, { paths: [ '.' ] }) throws without ./ #27583

@75lb

Description

@75lb
  • Version: v12.1.0
  • Platform: Darwin mba4.local 18.5.0 Darwin Kernel Version 18.5.0
  • Subsystem: Modules

I have a module lib/something.js which looks like this:

module.exports = function something () {}

In the node v10 REPL, this require.resolve invocation works fine:

> require.resolve('lib/something.js', { paths: [ '.' ] })
'/Users/lloyd/Documents/75lb/load-module/lib/something.js'

In node v12, it no longer works.

> require.resolve('lib/something.js', { paths: [ '.' ] })
Thrown:
Error: Cannot find module 'lib/something.js'
Require stack:
- /Users/lloyd/Documents/75lb/load-module/[eval]
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:610:15)
    at Function.resolve (internal/modules/cjs/helpers.js:21:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/lloyd/Documents/75lb/load-module/[eval]' ]
}

In node v12, you need to prepend ./ to the module ID like this:

> require.resolve('./lib/something.js', { paths: [ '.' ] })
'/Users/lloyd/Documents/75lb/load-module/lib/something.js'

This is an issue for my users because the path passed to require.resolve comes from command-line input, e.g.

$ example lib/something.js

Is this the expected new behaviour of require.resolve or a bug? Do I need to update my tools to prepend ./?

Metadata

Metadata

Assignees

No one assigned

    Labels

    moduleIssues and PRs related to the module subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions