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:03

if you are using windows , it takes some steps , 1) create a file called package.json

 {
  "name": "hello"
, "version": "0.0.1"
, "dependencies": {
    "express": "*"
  }
}

where hello is the name of the package and * means the latest version of your dependency

2) code to you project directory and run the following command

npm install

It installs the dependencies

查看更多
像晚风撩人
3楼-- · 2019-01-01 02:05

if you are in the windows7 platform maybe you should change the NODE_PATH like this: %AppData%\npm\node_modules

查看更多
冷夜・残月
4楼-- · 2019-01-01 02:06

From my expierience with win8.1 npm installs modules on C:\Users\[UserName]\AppData\Roaming\npm\node_modules but dumply searches them on C:\Users\[UserName]\node_modules.

One simple solution reference module in application by full path:

var jsonminify = require("C:/Users/Saulius/AppData/Roaming/npm/node_modules/jsonminify");
查看更多
时光乱了年华
5楼-- · 2019-01-01 02:10

Alternatively you could add to ~/.npmrc right prefix. I've got C:\Program Files\nodejs for 64 Win7.

查看更多
零度萤火
6楼-- · 2019-01-01 02:11

For Windows 10, I had to locally install gulp in the folder:

C:\Users\myaccount\AppData\Roaming\npm\node_modules

npm install gulp

This fixed my issue of "gulp is not recognized"

查看更多
残风、尘缘若梦
7楼-- · 2019-01-01 02:12

I had a terrible time getting global modules to work. Eventually, I explicitly added C:\Users\yourusername\AppData\Roaming\npm to the PATH variable under System Variables. I also needed to have this variable come before the nodejs path variable in the list.

I am running Windows 10.

查看更多
登录 后发表回答