Nodejs cannot find installed module on Windows

331,903

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:

  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?

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:

  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.
Share:
331,903

Related videos on Youtube

Cosmore
Author by

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, 2022

Comments

  • Cosmore
    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
      Amol M Kulkarni over 9 years
    • Dan Dascalescu
      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
      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
      Lonely almost 6 years
    • Sharif Yazdian
      Sharif Yazdian almost 5 years
      According to stackoverflow.com/questions/5817874/… npm install forever -g
    • Omar bakhsh
      Omar bakhsh almost 2 years
      fix path and try with another command line like powerShell
  • Cosmore
    Cosmore over 10 years
    It 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
    Cosmore over 10 years
    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.
  • Alexey Ivanov
    Alexey Ivanov over 10 years
    Glad to help. Didn't changing NODE_PATH broke path to standard node.js modules, like FS?
  • Gianfranco P.
    Gianfranco P. about 10 years
    It does now, as for version v0.8.9
  • pilau
    pilau over 9 years
    @beyonddoor Anything regarding Ivanov's question?
  • Mrchief
    Mrchief over 9 years
    @beyonddoor: You need to run npm link from a Administrator command window.
  • Swapnil Mhaske
    Swapnil Mhaske almost 9 years
    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
  • bryanmac
    bryanmac almost 9 years
    This doesn't install it globally (which the question asks). This pulls the dependency into the app
  • JonnyRaa
    JonnyRaa over 8 years
    it's in appdata\roaming on windows 8 aswell
  • Myk Willis
    Myk Willis almost 8 years
    Thanks @beyonddoor for pointing out that you cannot have quotation marks in NODE_PATH.
  • JaKXz
    JaKXz almost 8 years
    I'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
    Yar over 7 years
    in Windows 8 the path is %USERPROFILE%\AppData\Roaming\npm\node_modules
  • dpsthree
    dpsthree about 7 years
    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.
  • Benson
    Benson about 7 years
    Alternatively, for Windows 8 users %USERPROFILE%\AppData\Roaming\npm
  • UpTheCreek
    UpTheCreek almost 7 years
    They’re tiny JavaScript programs. Some of these libraries are certainly not tiny!
  • Phillip Copley
    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
    zipzit almost 7 years
    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?
  • cowlinator
    cowlinator over 6 years
    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}
  • danguilherme
    danguilherme about 6 years
    Thanks for the advice on removing the node_modules part, was struggling with this too!
  • gkiely
    gkiely about 6 years
    In windows 10 it should just be %AppData%\npm
  • Robot70
    Robot70 over 5 years
    Windows 8.1 pro with nodejs v6.9.4, to set the path run: C:\Program Files\nodejs\nodevars.bat
  • Dan Diplo
    Dan Diplo over 5 years
    Or, more simply, `%AppData%\npm`
  • Hinrich
    Hinrich over 5 years
    In my case (W10) it was %AppData%\Roaming\npm
  • Will
    Will over 5 years
    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.
  • salgmachine
    salgmachine over 5 years
    this is the only thing that worked for me. running on windows 8
  • Zhu Xiaohu
    Zhu Xiaohu almost 5 years
    yes just add to the path environment variabele, then we can run it directly in windows 10
  • Gabe Hiemstra
    Gabe Hiemstra over 4 years
    It would showing how to actually perform this with an example, instead of saying things such as "just add to..."
  • Ganesh Jadhav
    Ganesh Jadhav over 4 years
    Dear Google, please show this answer when any lost soul searches for: 'grunt' is not recognized as an internal or external command.
  • David Edwards
    David Edwards over 4 years
    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
    David Edwards over 4 years
    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.
  • YCode
    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
    devqon about 3 years
    This is the only thing that worked for me also, thank you!
  • Alex Hall
    Alex Hall over 1 year
    This approach only works for me if I use forward slashes instead.
  • Alex Hall
    Alex Hall over 1 year
    This is the only approach that worked for me but I had to use forward slashes in the path.
  • Ringo
    Ringo over 1 year
    This is a hack at best.
  • Ringo
    Ringo over 1 year
    whoa no need to take it personally. Im just saying if you work on this code with other people it's not gonna work
  • Saulius
    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
    Ringo over 1 year
    @saulius I can get on board with that! :-)