I am learning Node.js at the moment on Windows. Several modules are installed globally with npm.cmd, and Node.js 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'"
error,
var jade = require('jade');
However, the code will run successfully when Jade is locally installed (without the -g
option in npm). I don't want to use locally-installed modules; it's a waste of disk space for me. How can I make the globally-installed modules work on Windows?
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
).
A quick solution in Windows 7+ is to just run:
rem For the future
setx NODE_PATH %AppData%\npm\node_modules
rem For the current session
set NODE_PATH=%AppData%\npm\node_modules
It's worth to mention that NODE_PATH
is only used when importing modules in Node.js applications. When you want to use globally installed modules' binaries in your CLI you need to add it also to your PATH
, but without the node_modules
part (for example, %AppData%\npm
in Windows 7/8/10).
Old story
I'm pretty much new to Node.js myself, so I can be not entirely right, but from my experience it works this way:
See this similar question for more details: How do I install a module globally using npm?