21

I'm getting an error when trying to use any global module, exemple:

Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (C:\BitNami\wappstack\...\test\app.js)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)

I installed the express command:

npm install -g express

My app.js:

var express = require('express');

And run it using windows powershell or node.js command prompt windows:

node app.js

do not really know what's going wrong, I read something about environment variables in windows, can this be?

Resolved / Update

The problem was: Windows environment variables was not configured for npm folder. Search for your npm folder and add the path in the environment variables.

3
  • 1
    This happens to me as well and I work on a Linux box. Probably installing globally helps you to run express from any point by command line. If you run npm install in your projects directory and have express in your dependencies, it will work.
    – hgoebl
    Commented Jan 10, 2014 at 14:52
  • yes, but I intend to do a set of modules that I always use in various applications, then it would be a good idea having installed globally. Commented Jan 10, 2014 at 15:10
  • 1
    If you want to save space on your disk you can create a link to the globally installed module.
    – hgoebl
    Commented Jan 10, 2014 at 16:56

5 Answers 5

15

Just to quote from here:

https://www.npmjs.org/doc/files/npm-folders.html

  • Install it locally if you're going to require() it.
  • Install it globally if you're going to run it on the command line.
  • If you need both, then install it in both places, or use npm link.
2
  • 1
    Would prefer the command 1) npm install-locally somenodemodule and 2) another command npm install-globally-for-command-line somenodemodule or something in those terms ? Just a personal preference but I like it long readable explicit and juicy! Commented May 1, 2015 at 22:20
  • This totally cleared up the issue I was having. I just installed a library for use with require that I had in my mind to generally use quickly around my system, for no particular script or project. I had installed the library globally, then linked it locally into my working directory for quick use. I 'might' understand their paradigm, but would mostly like to still require it globally and easily.
    – Pysis
    Commented Sep 10, 2017 at 3:46
13

You should install Express locally:

npm install express

Then require it as you did:

var express = require('express')
5
  • 7
    I'm having trouble seeing what this answer says that isn't repetition of what the OP says in the question that he already did.
    – Adi Inbar
    Commented Apr 17, 2014 at 17:58
  • 6
    I'm also confused as to why this is the "accepted" answer, when the OP clearly stated he did something different and resolved the issue. Commented Jun 25, 2014 at 16:19
  • 2
    Install locally WHERE? Ex. /home/user/project?
    – basickarl
    Commented Jan 26, 2015 at 19:38
  • @AdiInbar in this user's case the difference is the global scope vs local scope (the -g flag) although Augie and Karl's points are valid Commented Oct 11, 2019 at 1:31
  • @Kari Morrison "locally" here likely means the working directory of the calling javascript file, the output of __dirname.Although you're absolutely correct this requires explicit clarification in the answer. Commented Oct 11, 2019 at 1:32
13

I was getting same error on Windows7/x64 and adding following in the environment variable resolved the issue:

NODE_PATH=C:\Users\[USERNAME]\AppData\Roaming\npm\node_modules

*Replace [USERNAME] with your actual system username

2
  • 3
    an alternative > set NODE_PATH=%USERPROFILE%\AppData\Roaming\npm\node_modules
    – Todd Smith
    Commented May 21, 2015 at 16:40
  • 3
    2nd alternative: set NODE_PATH=%AppData%\npm\node_modules
    – Markus
    Commented Jan 11, 2017 at 14:18
5

I'm working in Linux, but when I require express, I'm doing so with a relative path to where it is installed and it works fine:

var express = require('./public/node_modules/express');

I'm sure the same thing would work with a windows path as well. If you want to be more explicit and declare an absolute path, that would be the nuclear option to make sure you always know exactly where your module is being loaded from regardless of where your scripts are being run from.

If you still have a problem after using an explicit path, I don't know what the problem might be. . .

0

another option will be to run npm install --save express

Not the answer you're looking for? Browse other questions tagged or ask your own question.