Nodejs cannot find installed module on Windows?

2019-01-01 01:40发布

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?

17条回答
残风、尘缘若梦
2楼-- · 2019-01-01 02:13

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

enter image description here

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).

查看更多
永恒的永恒
3楼-- · 2019-01-01 02:14

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.

查看更多
余欢
4楼-- · 2019-01-01 02:16

Tried to add/edit environment variables and come to conclude that:

  1. Edit/add User variables (of the upper box) instead of System variables (of the lower part); otherwise you have to "run as administrator" to get it work.
  2. Append ;%AppData%\npm to Path in order to use it as a command line tool (if supported, like jshint and grunt-cli).
  3. Create NODE_PATH and set it %AppData%\npm\node_modules in order to require('<pkg_name>') in scripts without install it in the project directory. (But npm link is suggested for this requirement if you're working on OS with mklink such as Vista and newer.)

Test environment:

  • Win 7 (Ent., 64-bit, SP1), Node.js 4.2.4, npm 2.14.12
  • Win 8.1 (Ent., 64-bit), Node.js 0.10.30, npm 1.4.21
查看更多
谁念西风独自凉
5楼-- · 2019-01-01 02:16

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.

查看更多
骚的不知所云
6楼-- · 2019-01-01 02:17

I'll just quote from this node's blog post...

In general, the rule of thumb is:

  • If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
  • If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.

...

Of course, there are some cases where you want to do both. Coffee-script and Express both are good examples of apps that have a command line interface, as well as a library. In those cases, you can do one of the following:

  1. Install it in both places. Seriously, are you that short on disk space? It’s fine, really. They’re tiny JavaScript programs.
  2. Install it globally, and then npm link coffee-script or npm link express (if you’re on a platform that supports symbolic links.) Then you only need to update the global copy to update all the symlinks as well.
查看更多
登录 后发表回答