[windows] Nodejs cannot find installed module on Windows

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?

This question is related to windows node.js module require npm

The answer is


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.


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.


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


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");

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


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.


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.

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.


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


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


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


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

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.


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"


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


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.


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


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.


Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to module

How to fix: fatal error: openssl/opensslv.h: No such file or directory in RedHat 7 How to import functions from different js file in a Vue+webpack+vue-loader project Typescript ReferenceError: exports is not defined ImportError: No module named tensorflow ModuleNotFoundError: What does it mean __main__ is not a package? ES6 modules in the browser: Uncaught SyntaxError: Unexpected token import module.exports vs. export default in Node.js and ES6 What's the difference between an Angular component and module Export multiple classes in ES6 modules Python - Module Not Found

Examples related to require

The difference between "require(x)" and "import x" PHP - Failed to open stream : No such file or directory cannot redeclare block scoped variable (typescript) NodeJs : TypeError: require(...) is not a function Include PHP file into HTML file How to make node.js require absolute? (instead of relative) Ruby 'require' error: cannot load such file Nodejs cannot find installed module on Windows is there a require for json in node.js node.js require all files in a folder?

Examples related to npm

What does 'x packages are looking for funding' mean when running `npm install`? error: This is probably not a problem with npm. There is likely additional logging output above Module not found: Error: Can't resolve 'core-js/es6' Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist` ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean` What exactly is the 'react-scripts start' command? On npm install: Unhandled rejection Error: EACCES: permission denied Difference between npx and npm?