264

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. How can I make the globally-installed modules work on Windows?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Cosmore
  • 3,193
  • 4
  • 20
  • 22
  • http://stackoverflow.com/questions/15471965/what-will-be-the-difference-in-requiremypackage-js-and-requiremypackage/15471995#15471995 – Amol M Kulkarni Mar 26 '13 at 05:30
  • 3
    @AmolMKulkarni: not sure how that's relevant here. Adding '.js' to the require doesn't solve the problem on Windows. – Dan Dascalescu Feb 12 '14 at 12:50
  • 1
    @DanDascalescu: I think you misunderstood the answer. The code given in that answer is a code of Node.js, which shows how it looks for the package you require. So if you understand where and how it looks for files, you can then solve your issue more easily. – Amol M Kulkarni Feb 13 '14 at 10:24
  • https://github.com/npm/npm/wiki/Troubleshooting#upgrading-on-windows –  Dec 23 '16 at 09:52
  • According to https://stackoverflow.com/questions/5817874/how-do-i-install-a-module-globally-using-npm/5830153#5830153 npm install forever -g – Sharif Yazdian Jan 07 '18 at 19:18
  • fix path and try with another command line like powerShell – Omar bakhsh Dec 21 '20 at 01:50

21 Answers21

374

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

Quick solution in Windows 7+ is to just run:

rem for future
setx NODE_PATH %AppData%\npm\node_modules
rem for 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 apps. When you want to use globally installed modules' binaries in your CLI you need to add it also to your PATH, but without 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'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: http://nodejs.org/api/modules.html#modules_file_modules (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?

Wirone
  • 3,304
  • 1
  • 29
  • 48
Alexey Ivanov
  • 8,128
  • 2
  • 26
  • 27
  • 17
    Thanks very much, the links you provided are very helpful, I am much clearer now, although the module-searching algorithm looks a bit complicated. Finally I use the environment variable NODE_PATH to reference the global module path, `set NODE_PATH=C:\Documents and Settings\DevUser\Application Data\npm\node_modules`, and it works as expected. It's strange that globally installed modules are positioned in '%USERPROFILE%\Application Data\npm\node_modules' on Windows, meanwhile, there cannot be quotation marks in NODE_PATH. – Cosmore Mar 07 '12 at 07:09
  • 2
    Glad to help. Didn't changing NODE_PATH broke path to standard node.js modules, like FS? – Alexey Ivanov Mar 07 '12 at 08:27
  • @beyonddoor Anything regarding Ivanov's question? – pilau Jul 25 '13 at 11:02
  • 2
    Even I was wondering why my `C:\Program Files\nodejs\node_modules\npm\node_modules` do not have the module which I just installed using `npm install -g express` and It was available in `C:\Documents and Settings\swapnil\Application Data\npm\node_modules` – Swapnil Mhaske Feb 17 '14 at 11:53
  • Thanks @beyonddoor for pointing out that you cannot have quotation marks in NODE_PATH. – Myk Willis Dec 08 '14 at 01:07
  • 1
    in Windows 8 the path is `%USERPROFILE%\AppData\Roaming\npm\node_modules` – Yar Mar 18 '15 at 21:45
  • 9
    If you want to find out where your global node_modules directory is, run the command `npm list -g`. The first line of output will be the parent of the global node_modules directory-- in other words, the global node_modules directory is `{output}\node_modules`. It also prints out the install directory when you run `npm install --global {xyz}` – cowlinator Mar 28 '16 at 21:39
  • Dear Google, please show this answer when any lost soul searches for: 'grunt' is not recognized as an internal or external command. – Ganesh Jadhav Jul 11 '18 at 10:47
  • @cowlinator Thank you for `npm list -g`--I've been searching all over for ways to figure out if npm had indeed installed gulp, even read an article titled "An Absolute Beginner's Guide to Using npm" and found nothing. It's the small things that elude beginners. – YCode Feb 07 '19 at 03:14
41

I know i can awake a zombie but i think this is still a problem, if you need global access to node modules on Windows 7 you need to add this to your global variable path:

C:\Users\{USER}\AppData\Roaming\npm

Important: only this without the node_modules part, took me half hour to see this.

Boaz
  • 19,892
  • 8
  • 62
  • 70
Vitaliy Terziev
  • 6,429
  • 3
  • 17
  • 25
34

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

mark.monteiro
  • 2,609
  • 2
  • 33
  • 38
Jack
  • 357
  • 3
  • 4
28

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.

Andi-lo
  • 2,244
  • 21
  • 26
  • 7
    Or, more simply, `%AppData%\npm\` – Dan Diplo Mar 30 '17 at 08:53
  • Man it's weird that worked! I've been hunting this issue for two days, literally. This is the most obscure solution I've tried and the first one that worked. Thank you so much. – Will May 11 '17 at 14:18
  • yes just add to the `path` environment variabele, then we can run it directly in windows 10 – Zhu Xiaohu Dec 18 '17 at 15:47
  • 1
    It would showing how to actually perform this with an example, instead of saying things such as "just add to..." – Gabe Hiemstra Jul 02 '18 at 09:32
14

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.
marko
  • 1,721
  • 15
  • 25
12

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
Kong Kao
  • 173
  • 2
  • 10
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.

Josh Weston
  • 1,632
  • 22
  • 23
12

To make it short, use npm link jade in your app directory.

Sơn Trần-Nguyễn
  • 2,188
  • 1
  • 26
  • 30
6

I ran into this issue on Windows 7, running

npm install -g gulp

as administrator while being logged on as a normal user.

Solution: When executing the same installation as normal user (not "run as admin" for cmd) all was fine. I guess it is related to the default install and search path.

SCBuergel
  • 1,613
  • 16
  • 26
  • 1
    To clarify this point a bit more... When Node is installed as an administrator it updates the system path to include a reference to the administrators global node module folder. When a new user install something globally it installs to a different node module folder. Each user other than the user that installed Node will need to update their path to include their global node module folder. If you install and use node on the same account this will not be necessary. – dpsthree Oct 22 '15 at 15:39
  • Windows 8.1 pro with nodejs v6.9.4, to set the path run: C:\Program Files\nodejs\nodevars.bat – Robot70 Mar 06 '17 at 14:14
4

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");
Saulius
  • 1,736
  • 20
  • 29
  • This approach only works for me if I use forward slashes instead. – Alex Hall Mar 24 '21 at 17:00
  • This is a hack at best. – Ringo May 28 '21 at 01:39
  • whoa no need to take it personally. Im just saying if you work on this code with other people it's not gonna work – Ringo Jun 02 '21 at 17:34
  • 1
    @Ringo apparently. And there are many ways to solve that. But if you just pilloting smth like I was it can at least point you to the right direction. From my expierence you start with unperfect code and then improving it. – Saulius Jun 03 '21 at 06:28
  • 1
    @saulius I can get on board with that! :-) – Ringo Jun 03 '21 at 16:14
4

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.

danwellman
  • 9,068
  • 8
  • 60
  • 88
Kiki.J.Hu
  • 204
  • 2
  • 6
4

For me worked on Windows 10 npm config set prefix %AppData%\npm\node_modules

Bojan Mitic
  • 462
  • 5
  • 12
3

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

Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35
Gregorio
  • 393
  • 3
  • 9
1

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

dmi3y
  • 3,482
  • 2
  • 21
  • 32
1

I stumbled on this question because I want to use node.js with visual studio 2015 on my new computer with windows 10. I used node.js on windows 7 and 8 and 8.1 Never a problem node.js finding a module. I use a legacy node.js 0.10.39 because I have to use this version because of the serial and RFXCOM module.

The answer for windows 10 is to set the NODE_PATH in the enviroment variables with C:\Users\User\node_modules.

wetlip
  • 133
  • 3
  • 19
  • 1
    Er, no. (Windows 10 here..) I see some modules at `C:\Users\User\node_modules` I see some modules at `C:\Users\User\AppData\Roaming\npm\node_modules` I see some modules at `C:\Users\User\node_modules` Also located at `C:\Program Files (x86)\nodejs\node_modules\npm\node_modules` Not sure how this happens. Which is which, and why'd they do it that way? – zipzit Feb 27 '16 at 09:19
1

I had to add following to Path variable under System variables. Setting variable under User variable did not work for me. I am using windows 11.

%USERPROFILE%\AppData\Roaming\npm

Anonymous Creator
  • 2,968
  • 7
  • 31
  • 77
0

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"

0

Just download and re-install the node from this and this will fix all the path issues.

Don't forget to restart your command prompt or terminal.

Mahendra
  • 447
  • 6
  • 7
0

All of the above answers did not work for me. The only thing that worked eventually was to add the %AppData%\npm to the environment Path variable, AND to delete the two ng files in C:\Program Files\nodejs.

The ng packages were not installed in C:\Program Files\nodejs\node_modules, so it was apparent that using the ng binary from the nodejs directory would not work.

I am not sure why it searched in this directory, because I already configured - PATH environment variable - .npmrc in the C:\Users\MyUser - Tried to add system variables and/or NODE_PATH

devqon
  • 13,818
  • 2
  • 30
  • 45
0

I was trying pnpm prisma generate and npm prisma generate and none of them worked. But then I tried with the npx command, like below.

npx prisma generate

And this line of code saves me, it sounds a little bit weird but it does working atleast on my project.

-4

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

Subbu
  • 2,063
  • 4
  • 29
  • 42
  • 4
    This doesn't install it globally (which the question asks). This pulls the dependency into the app – bryanmac Feb 17 '14 at 14:10
  • I've tried everything listed above to try and persuade my node.js installation to recognise globally installed modules, and NONE of it works. Even after uninstalling and reinstalling node (v8.11.3 LTS), every time I try to issue the require("mongodb") command in a node.js script, I get the SAME error: "Cannot find module mongodb". Yet, if I issue the command "npm ll -g mongodb", NPM tells me the module is installed. Can someone PLEASE hand me some infirmation that WORKS with this problem? – David Edwards Jul 24 '18 at 21:54
  • UPDATE: Saulius above has provided a workaround that actually works, namely specifying the full path to the module in the JavaScript require statement. But if his stated observation about node.js searching in the wrong folders for installed modules is correct, can someone alert the node.js developers to this? Because the problem is STILL affecting v8.11.3 LTS. – David Edwards Jul 25 '18 at 10:51