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'"
error,
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?
I had the same issue, trying to install bower with
npm install -g bower
I think this was because node was installed by another user, not me.
I uninstalled node, and then I reinstalled it. During installation, I saw this text for the option Add to PATH > npm modules:
Message in node installation
After node installation, I executed
npm install -g bower
again. And now bower works.Sure is not necessary reinstall node with own user, like me. Solution must be via NODE_PATH or PATH variables, as other users have explained.
This is only to remark that this problem occurs only if node has been installed by another user (or if during installation the option Add to PATH > npm modules has not been marked).
For making it work on windows 10 I solved it by adding the folder
%USERPROFILE%\AppData\Roaming\npm
to my PATH. Having\node_modules
appended like this:%USERPROFILE%\AppData\Roaming\npm\node_modules\
did not work for me.Tried to add/edit environment variables and come to conclude that:
User variables
(of the upper box) instead ofSystem variables
(of the lower part); otherwise you have to "run as administrator" to get it work.;%AppData%\npm
toPath
in order to use it as a command line tool (if supported, likejshint
andgrunt-cli
).NODE_PATH
and set it%AppData%\npm\node_modules
in order torequire('<pkg_name>')
in scripts without install it in the project directory. (Butnpm link
is suggested for this requirement if you're working on OS withmklink
such as Vista and newer.)Test environment:
For windows, everybody said you should set environment variables for nodejs and npm modules, but do you know why? For some modules, they have command line tool, after installed the module, there'are [module].cmd file in C:\Program Files\nodejs, and it's used for launch in window command. So if you don't add the path containing the cmd file to environment variables %PATH% , you won't launch them successfully through command window.
I'll just quote from this node's blog post...