Node: 12.3.0 introduces a breaking change in the way require.resolve's options.paths is validated

Created on 22 May 2019  路  4Comments  路  Source: nodejs/node

  • Version: 12.3.0
  • Platform: Linux localhost 4.19.0-5-amd64 #1 SMP Debian 4.19.37-3 (2019-05-15) x86_64 GNU/Linux
  • Subsystem: Modules

Steps to reproduce

Save the following file as test.js:

console.log(require.resolve("./test", { paths: "." }));

Run it:

node test.js

Expected Behavior

This output on stdout:

/tmp/t1/test.js

Actual Behavior

In node 12.3.0 this is what I get:

internal/modules/cjs/loader.js:603
      throw new ERR_INVALID_OPT_VALUE('options.paths', options.paths);
      ^

TypeError [ERR_INVALID_OPT_VALUE]: The value "." is invalid for option "options.paths"
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:13)
    at Function.resolve (internal/modules/cjs/helpers.js:21:19)
    at Object.<anonymous> (/tmp/t1/test.js:1:21)
    at Module._compile (internal/modules/cjs/loader.js:774:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    at Module.load (internal/modules/cjs/loader.js:641:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:837:10)
    at internal/main/run_main_module.js:17:11

Observations

I get the expected behavior in Node 12.2.0, Node 10.14.2 and Node 8.15.0.

The documentation for require.resolve has been at least as far back as Node 8 that an array must be passed for the paths option. However, in reality, Node has been accepting a string for paths, without complaint, for quite a while.

The upshot is that code that worked just fine with Node prior to 12.3.0 breaks with 12.3.0.

Most helpful comment

All bug fixes change behavior in some way. Sometimes it's a grey area, and this one probably could have gone either way. I do apologize for any inconvenience that it causes you, but I think that resolving modules to the wrong location seems like a pretty significant bug.

All 4 comments

The previous behavior wasn't working at all. It was problematic to silently ignore the problem too because it made it possible to get incorrect results.

I get that there was a problem with the way paths was handled. The problem now though is that a minor version update breaks code that used to work. I thought Node follows semver, which mandates that breaking changes happen with major releases.

I ran into the issue with a third-party tool I use. Whatever problems there existed before, they did not prevent this tool from running just fine with Node 12.x prior to 12.3.0.

All bug fixes change behavior in some way. Sometimes it's a grey area, and this one probably could have gone either way. I do apologize for any inconvenience that it causes you, but I think that resolving modules to the wrong location seems like a pretty significant bug.

Closing given that there's really not much actionable here.

Was this page helpful?
0 / 5 - 0 ratings