Skip to content

module: fix loading from global folders on Windows

Checklist
  • make -j8 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

module

Description of change

According to the module documentation:

Additionally, Node.js will search in the following locations:

    1: $HOME/.node_modules
    2: $HOME/.node_libraries
    3: $PREFIX/lib/node

Where $HOME is the user's home directory, and $PREFIX is Node.js's configured node_prefix.

Loading from $PREFIX/lib/node is broken on Windows as the code is calculating $PREFIX/lib/node relative to process.execPath, but on Windows process.execPath is $PREFIX\node.exe whereas everywhere else process.execPath is $PREFIX/bin/node (where $PREFIX is the root of the installed Node.js).

e.g. If Node.js is in c:\node, the code is adding c:\lib\node to the lookup path instead of c:\node\lib\node:

C:\node>set NODE_DEBUG=module

C:\node>node nosuchfile
MODULE 12780: looking for "C:\\node\\nosuchfile" in ["C:\\Users\\IBM_ADMIN\\.nod
e_modules","C:\\Users\\IBM_ADMIN\\.node_libraries","C:\\lib\\node"]
module.js:472
    throw err;
    ^

Error: Cannot find module 'C:\node\nosuchfile'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3

C:\node>

Merge request reports

Loading