Cosmore Cosmore - 1 year ago 98
Node.js Question

Nodejs cannot find installed module on Windows?

I am learning nodejs at the moment on Windows. Several modules are installed globally with npm.cmd, and nodejs failed to find the installed modules. Take jade for example,

npm install jade -g

Jade is installed in directory
"C:\Program Files (x86)\nodejs\node_modules"
, but the following code will fail with a
"Cannot find module 'jade'"

var jade = require('jade');

However, the code will run successfully when jade is locally installed (without -g option in npm). I don't want to use locally-installed modules, it's a waste of disk space for me, can someone help me to make the globally-installed modules work on Windows?

Answer Source

Add an environment variable called NODE_PATH and set it to %USERPROFILE%\Application Data\npm\node_modules (Windows XP), %AppData%\npm\node_modules (Windows 7/8/10), or wherever npm ends up installing the modules on your Windows flavor. To be done with it once and for all, add this as a System variable in the Advanced tab of the System Properties dialog (run control.exe sysdm.cpl,System,3).

Old story

I'm pretty much new to node.js myself so I can be not entirely right but from my experience it's works this way:

  1. -g is not a way to install global libraries, it's only a way to place them on system path so you can call them from command line without writing the full path to them. It is useful, for example, then node app is converting local files, like less — if you install it globally you can use it in any directory.
  2. node.js itself didn't look at the npm global dir, it is using another algorithm to find required files: (basically its scanning every folder in the path, starting from the current for node_modules folder and checks it).

See similar question for more details: How do I install a module globally using npm?