Nodejs cannot find installed module on Windows
Solution 1
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:
- -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.
- 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?
Solution 2
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.
Solution 3
if you are in the windows7 platform maybe you should change the NODE_PATH like this:
%AppData%\npm\node_modules
Solution 4
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.
Solution 5
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:
- Install it in both places. Seriously, are you that short on disk space? It’s fine, really. They’re tiny JavaScript programs.
- 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.
Related videos on Youtube

Cosmore
A happy programmer who always find some wonderful things everyday, he thanks the great people who made the world so fantastic and wish to join and learn from these masters.
Updated on March 12, 2022Comments
-
Cosmore 9 months
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?
-
Amol M Kulkarni over 9 years
-
Dan Dascalescu almost 9 years@AmolMKulkarni: not sure how that's relevant here. Adding '.js' to the require doesn't solve the problem on Windows.
-
Amol M Kulkarni almost 9 years@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.
-
Lonely almost 6 years
-
Sharif Yazdian almost 5 yearsAccording to stackoverflow.com/questions/5817874/… npm install forever -g
-
Omar bakhsh almost 2 yearsfix path and try with another command line like powerShell
-
-
Cosmore over 10 yearsIt seems
npm link jade
can't work on Windows, npm returns an error message as follows:npm ERR! Error: npm link not supported on windows
-
Cosmore over 10 yearsThanks 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. -
Alexey Ivanov over 10 yearsGlad to help. Didn't changing NODE_PATH broke path to standard node.js modules, like FS?
-
Gianfranco P. about 10 yearsIt does now, as for version v0.8.9
-
pilau over 9 years@beyonddoor Anything regarding Ivanov's question?
-
Mrchief over 9 years@beyonddoor: You need to run
npm link
from a Administrator command window. -
Swapnil Mhaske almost 9 yearsEven I was wondering why my
C:\Program Files\nodejs\node_modules\npm\node_modules
do not have the module which I just installed usingnpm install -g express
and It was available inC:\Documents and Settings\swapnil\Application Data\npm\node_modules
-
bryanmac almost 9 yearsThis doesn't install it globally (which the question asks). This pulls the dependency into the app
-
JonnyRaa over 8 yearsit's in appdata\roaming on windows 8 aswell
-
Myk Willis almost 8 yearsThanks @beyonddoor for pointing out that you cannot have quotation marks in NODE_PATH.
-
JaKXz almost 8 yearsI'm on windows 8.1 with npm 2.5.1, and I've got my NODE_PATH set as documented, but it still can't find my installed packages. Thoughts?
-
Yar over 7 yearsin Windows 8 the path is
%USERPROFILE%\AppData\Roaming\npm\node_modules
-
dpsthree about 7 yearsTo 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.
-
Benson about 7 yearsAlternatively, for Windows 8 users %USERPROFILE%\AppData\Roaming\npm
-
UpTheCreek almost 7 years
They’re tiny JavaScript programs.
Some of these libraries are certainly not tiny! -
Phillip Copley almost 7 years@UpTheCreek I'm confident that he was referring to size in the scope of disk space. They are text files. They are objectively small.
-
zipzit almost 7 yearsEr, no. (Windows 10 here..) I see some modules at
C:\Users\User\node_modules
I see some modules atC:\Users\User\AppData\Roaming\npm\node_modules
I see some modules atC:\Users\User\node_modules
Also located atC:\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? -
cowlinator over 6 yearsIf 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 runnpm install --global {xyz}
-
danguilherme about 6 yearsThanks for the advice on removing the
node_modules
part, was struggling with this too! -
gkiely about 6 yearsIn windows 10 it should just be %AppData%\npm
-
Robot70 over 5 yearsWindows 8.1 pro with nodejs v6.9.4, to set the path run: C:\Program Files\nodejs\nodevars.bat
-
Dan Diplo over 5 yearsOr, more simply, `%AppData%\npm`
-
Hinrich over 5 yearsIn my case (W10) it was %AppData%\Roaming\npm
-
Will over 5 yearsMan 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.
-
salgmachine over 5 yearsthis is the only thing that worked for me. running on windows 8
-
Zhu Xiaohu almost 5 yearsyes just add to the
path
environment variabele, then we can run it directly in windows 10 -
Gabe Hiemstra over 4 yearsIt would showing how to actually perform this with an example, instead of saying things such as "just add to..."
-
Ganesh Jadhav over 4 yearsDear Google, please show this answer when any lost soul searches for: 'grunt' is not recognized as an internal or external command.
-
David Edwards over 4 yearsI'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 over 4 yearsUPDATE: 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.
-
YCode almost 4 years@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. -
devqon about 3 yearsThis is the only thing that worked for me also, thank you!
-
Alex Hall over 1 yearThis approach only works for me if I use forward slashes instead.
-
Alex Hall over 1 yearThis is the only approach that worked for me but I had to use forward slashes in the path.
-
Ringo over 1 yearThis is a hack at best.
-
Ringo over 1 yearwhoa no need to take it personally. Im just saying if you work on this code with other people it's not gonna work
-
Saulius over 1 year@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.
-
Ringo over 1 year@saulius I can get on board with that! :-)