require has issues when run from /

  • Version: 6.1.0
  • Platform: Darwin ReBuke-Pro.local 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64 and Linux f50a089ff63e 4.4.8-boot2docker #1 SMP Mon Apr 25 21:57:27 UTC 2016 x86_64 Linux
  • Subsystem: require/modules

The following command works in v5/v4/v0.12/v0.10 with npm@2.15.5 or npm@3.8.9, works in v6 with npm@2.15.5, but fails in v6 with npm@3.8.9:

$ cd /
$ sudo npm install express
$ sudo node -p "require('express')"
...
module.js:440
    throw err;
    ^
Error: Cannot find module 'merge-descriptors'
    at Function.Module._resolveFilename (module.js:438:15)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/node_modules/express/lib/express.js:16:13)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)

The following command works in v6 (and below):

$ sudo mkdir /app && cd /app && sudo npm install express && sudo node -p "require('express')"

You can reproduce this fairly easily without messing up your filesystem if you have docker (can use mhart/alpine-node instead of node below if you want a smaller download):

$ docker run node:5 sh -c 'npm install express && node -p "require(\"express\")"'

(^works)

$ docker run node:6 sh -c 'npm install express && node -p "require(\"express\")"'

(^doesn't work)

$ docker run node:6 sh -c 'mkdir app && cd app && npm install express && node -p "require(\"express\")"'

(^works)

It's unclear to me whether it's the fact that it's being run from the root directory specifically that's causing it to fail – or whether it's the presence of one of the other directories (eg, bin) that's getting in the way