0

Fresh Ubuntu 16.04 installation. Installed nodejs 10.15 according to the official instructions

# curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
# apt-get install nodejs

Then installed ws globally

# npm install -g ws
+ ws@6.1.3
updated 1 package in 0.374s

Then in a js file I do:

const WebSocket = require('ws');

Then run it:

# nodejs ws.js
internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module 'ws'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/var/nfs/nodes/api/arbitrage/hotbit.js:1:81)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

It appears the global modules are installed in /usr/lib/node_modules but apps still fail unless I manually say

export NODE_PATH=/usr/lib/node_modules

Is this a bug?

Normadize
  • 1,156
  • 12
  • 22
  • I believe this is by design, see here: https://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package – jaseeey Jan 29 '19 at 14:31
  • This to me looks either like a bug or a poor design choice to have `-g` install modules into `/usr/lib/node_modules` when actually `node` doesn't look for modules there, but **does** look for modules in `$PREFIX/lib/node` (`$PREFIX` for the Ubuntu package installation is `/`) ... – Normadize Jan 29 '19 at 16:01

0 Answers0