244

I wrote a module which I published to npm a moment ago (https://npmjs.org/package/wisp)

So it installs fine from the command line:

$ npm i -g wisp

However, when I run it from the command line, I keep getting an error that optimist isn't installed:

$ wisp 
Error: Cannot find module 'optimist'
    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> (/usr/local/lib/node_modules/wisp/wisp:12:10)
    at Object.<anonymous> (/usr/local/lib/node_modules/wisp/wisp:96:4)
    at Module._compile (module.js:449:26)
    at Object.exports.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:68:25)
    at compileScript (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:135:29)
    at fs.stat.notSources.(anonymous function) (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:110:18)

However, I have specified in package.json as a dependancy:

{
  "name": "wisp",
  "author": "Brendan Scarvell <bscarvell@gmail.com>",
  "version": "0.1.0",
  "description": "Global nodejs file server",
  "dependencies": {
    "optimist": "~0.3.4"
  },
  "repository": "git://github.com/tehlulz/wisp",
  "bin": {
    "wisp" : "./wisp"
  }
}

Does anyone know what to do to get this running? I know its to do with the bin part adding the executable to bin and the node_modules in that directory being empty. No idea how to resolve this.

Menztrual
  • 40,867
  • 12
  • 57
  • 70

19 Answers19

417

For anyone else running into this, I had this problem due to my npm installing into a location that's not on my NODE_PATH.

[root@uberneek ~]# which npm
/opt/bin/npm
[root@uberneek ~]# which node
/opt/bin/node
[root@uberneek ~]# echo $NODE_PATH

My NODE_PATH was empty, and running npm install --global --verbose promised-io showed that it was installing into /opt/lib/node_modules/promised-io:

[root@uberneek ~]# npm install --global --verbose promised-io
npm info it worked if it ends with ok
npm verb cli [ '/opt/bin/node',
npm verb cli   '/opt/bin/npm',
npm verb cli   'install',
npm verb cli   '--global',
npm verb cli   '--verbose',
npm verb cli   'promised-io' ]
npm info using npm@1.1.45
npm info using node@v0.8.4
[cut]
npm info build /opt/lib/node_modules/promised-io
npm verb from cache /opt/lib/node_modules/promised-io/package.json
npm verb linkStuff [ true, '/opt/lib/node_modules', true, '/opt/lib/node_modules' ]
[cut]

My script fails on require('promised-io/promise'):

[neek@uberneek project]$ node buildscripts/stringsmerge.js 

module.js:340
    throw err;
          ^
Error: Cannot find module 'promised-io/promise'
    at Function.Module._resolveFilename (module.js:338:15)

I probably installed node and npm from source using configure --prefix=/opt. I've no idea why this has made them incapable of finding installed modules. The fix for now is to point NODE_PATH at the right directory:

export NODE_PATH=/opt/lib/node_modules

My require('promised-io/promise') now succeeds.

Neek
  • 7,181
  • 3
  • 37
  • 47
  • 14
    where to set this NODE_PATH ?? – sandy Aug 29 '13 at 06:45
  • 10
    @sandy where you set the NODE_PATH environment variable depends on your operating system. I'm using a flavour of Linux, so I used `export NODE_PATH=...` to set it in the local shell such that it is exported to commands run in that shell. To make the change persistent or available to all shells, put it in .profile or similar. The concept as a whole is annoyingly complex to the newbie, try reading https://help.ubuntu.com/community/EnvironmentVariables . For Windows, you'll want to switch to Linux. OK, just kidding. Try http://www.computerhope.com/issues/ch000549.htm or google it. – Neek Dec 14 '13 at 17:23
  • 14
    For OSX Lion, following worked for me ... export NODE_PATH=/usr/local/lib/node_modules I used 'locate node_modules' to track down the right path. – k1eran Dec 19 '13 at 16:09
  • 2
    NODE_PATH works for WINDOWS as well. this is rooted in node modules can be installed both locally and globally. – zinking May 20 '14 at 14:33
  • `find /bin /opt /usr -name node_modules 2>/dev/null` to find module location Note: on centos 6 ended up being in /usr/lib/node_modules--setting path with export worked – sjt003 May 21 '14 at 14:38
  • 1
    mac os x `/usr/local/lib/node_modules/`. YMMV. – Kinjal Dixit Jun 20 '14 at 09:08
  • Brew: `export NODE_PATH=/usr/local/share/npm/lib/node_modules` – shredding Jun 25 '14 at 16:27
  • 5
    If you installed Node.js with Homebrew on Mac OS X: Run `open -a TextEdit ~/.bash_profile` in Terminal, then add `export NODE_PATH=/usr/local/lib/node_modules` to the end of the file. Restart the terminal window. Now running `echo $NODE_PATH` should print out `/usr/local/lib/node_modules`. – Pwdr Dec 26 '14 at 14:11
  • you are super awesome :) It worked for me. Basically just point the node path to the place where all your node modules are installed. – Ankit Tanna Jan 20 '15 at 14:44
  • 2
    nice! this worked perfectly! for my setup - OSX 10.10 - i updated my ~/.bash_profile and added # correct path for global node-modules export NODE_PATH=/usr/local/lib/node_modules/ then you can reload your profile with 'source ~/.bash_profile' and everything should work fine! – Johannes Ferner Feb 07 '15 at 20:16
  • Works perfectly on Linux Ubuntu 14.04. Thanks! – Seth Jun 29 '15 at 19:17
  • After change the `NODE_PATH` what happens with local file? if i want to require a js file in project folder? – TomSawyer Aug 11 '16 at 18:35
  • `TypeError: app.configure is not a function` – TomSawyer Aug 11 '16 at 18:37
  • For OSX run `locate "lib/node_modules"` to find out where npm is installing modules. Then run `export NODE_PATH=/[your-directory]/lib/node_modules` – Timothy Sep 19 '16 at 08:18
  • `echo 'export NODE_PATH="$NVM_DIR/versions/node/\`nvm current\`/lib/node_modules' >> ~/.zshrc && source ~/.zshrc` – elquimista Jan 13 '17 at 08:40
  • 1
    Users of nvm may find this related StackOverflow question helpful (Configuring $NODE_PATH with NVM): https://stackoverflow.com/questions/27876557/node-js-configuring-node-path-with-nvm – varogen Jan 14 '23 at 20:38
51

add this to beginning of prog(mac):

module.paths.push('/usr/local/lib/node_modules');

Dio Chou
  • 653
  • 5
  • 4
  • 4
    Any way of doing that in a non-os specific way? – UpTheCreek Jun 24 '15 at 11:16
  • @UpTheCreek this **is** a non-os specific way. It's JS code, you just need to point to a node_modules that has the moduleyou are looking for – Adelin Jan 03 '18 at 11:20
  • 1
    @Adelin - It's an OS specific solution because the location of global node_modules is OS specific. E.g. if you are developing on windows, and running linux in production, then this is not a great solution. – UpTheCreek Jan 04 '18 at 11:28
  • That's easily overcome. For example you can save the node path in an env variable, for example, and replace the hardcoded path with `os.environ.nodepath` (something along these lines). – Adelin Jan 04 '18 at 11:30
  • macOS Mojave 10.14.6 requires adding export NODE_PATH=/usr/local/lib/node_modules/npm/node_modules to ~/bash_profile which is more machine independent in that the native .js code is unaffected. – vwvan Sep 10 '19 at 07:03
  • You can get your global node modules path by running `npm root -g` and then using the solution above in your JS file. – Olamide226 Feb 08 '23 at 13:45
17

By default node does not look inside the /usr/local/lib/node_module for loading global modules. Refer the module loading explained in http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders

So either you have to 1)add the /usr/local/lib/node_module to NODE_PATH and export it or 2)copy the installed node modules to /usr/local/lib/node . (As explained in the link for loading module node will search in this path and will work)

randomness
  • 1,377
  • 1
  • 14
  • 21
15

The following generic fix would for any module. For example with request-promise.

Replace

npm install request-promise --global

With

npm install request-promise --cli

worked (source) and also for globals and inherits

Also, try setting the environment variable

NODE_PATH=%AppData%\npm\node_modules
MonoThreaded
  • 11,429
  • 12
  • 71
  • 102
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
11

For some (like me) that nothing else worked, try this:

brew cleanup
brew link node
brew uninstall node
brew install node

Hope it helps someone :)

Will Silveira
  • 383
  • 2
  • 8
8

I got the "optimist" module error and I just did "npm install" to resolve it. went past that error.

https://github.com/mbloch/mapshaper/issues/12

user2921139
  • 1,669
  • 4
  • 17
  • 25
3

On windows if you just did a clean install and you get this you need blow away your npm cache in \AppData\Roaming

Dmitry
  • 1,513
  • 1
  • 15
  • 33
3

$ vim /etc/profile.d/nodejs.sh

export NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
user1959076
  • 411
  • 8
  • 18
  • That is there by default. For users it's loaded, but while run as root it's not, therefore needs to be included in root user's path. – stamster Mar 01 '17 at 13:38
2

For Windows, from Nodejs cannot find installed module on Windows? what worked for me is running npm link as in

npm link wisp
Community
  • 1
  • 1
P M
  • 33
  • 3
  • A word of caution though - I did this but could not then use my zipped code with AWS lambda. I had to have the node_module created locally as in `npm install wisp --save` (without the -g option) – P M Aug 08 '16 at 01:23
2

I did this in simple way...

  1. Un-Install node from control panel [Windows 7]
  2. Install node again
  3. Install protractor npm install --global --verbose protractor
    Update web driver manager.

works fine for me.

Hope this helps you....

Alex Sorokoletov
  • 3,102
  • 2
  • 30
  • 52
santhosh v
  • 31
  • 3
2

I got this error Error: Cannot find module 'number-is-nan' whereas the module actually exists. It was due to a bad/incomplete Node.js installation.

For Windows , as other answers suggest it, you need a clean Node installation :

  • Uninstall Node.js
  • Delete the two folders npm and npm_cache in C:\Users\user\AppData\Roaming
  • Restart Windows and install Node.js
  • Run npm initor (npm init --yes for default config)
  • Set the Windows environment variable for NODE_PATH. This path is where your packages are installed. It's probably something likeNODE_PATH = C:\Users\user\node_modules or C:\Users\user\AppData\Roaming\npm\node_modules
  • Start a new cmd console and npm should work fine

Note :

Try the last points before reinstalling Node.js, it could save you some time and avoid to re-install all your packages.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
2

For Mac User's It's Best use the manual installation:

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, it will be a hidden directory on your home folder.

  1. Back-up your computer before you start.

  2. Make a directory for global installations:

    mkdir ~/.npm-global

  3. Configure npm to use the new directory path:

    npm config set prefix '~/.npm-global'

  4. Open or create a ~/.profile file and add this line:

    export PATH=~/.npm-global/bin:$PATH

  5. Back on the command line, update your system variables:

    source ~/.profile

  6. Test: Download a package globally without using sudo.

    npm install -g jshint

Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global

Reference : https://docs.npmjs.com/getting-started/fixing-npm-permissions

Sjon
  • 4,989
  • 6
  • 28
  • 46
2

I have just met this problem of the axios module. Then I tried this: run rm -rf node_modules and yarn. And it works.

Finedy
  • 21
  • 1
1

Had the same problem on one of the test servers running Ubuntu under root. Then created a new user using useradd -m myuser and installed everything (nvm, node, packages) as myuser. Now it's working fine.

Dmytro
  • 5,443
  • 2
  • 52
  • 50
1

In my case both node and npm were in same path (/usr/bin). The NODE_PATH was empty, so the npm placed the global modules into /usr/lib/node_modules where require(...) successfully find them. The only exception was the npm module, which came with the nodejs package. Since I'm using 64 bit system, it was placed into /usr/lib64/node_modules. This is not where require(...) searches in case of empty NODE_PATH and node started from /usr/bin. So I had two options:

  • link /usr/lib64/node_modules/npm to /usr/lib/node_modules/npm
  • move modules from /usr/lib/node_modules/* to /usr/lib64/node_modules/ and set NODE_PATH=/usr/lib64/node_modules

Both worked. I'm using OpenSUSE 42.1 and the nodejs package from updates repository. Version is 4.4.5.

GT.
  • 91
  • 6
0

I had the same error as the OP, but digging through the logs I could see sh: node: command not found.

It turns out that the /usr/bin/node program (symlink) is no longer installed with apt install nodejs. Once symlinked /usr/bin/node' tonodejs,npm install -g @angular/cli` succeeded.

The proper way to install this on debian is apt install nodejs-legacy.

some bits flipped
  • 2,592
  • 4
  • 27
  • 42
0

I had to add C:\Users\{Username}\AppData\Roaming\npm to my env variables and then i could install stuff.

Rainhider
  • 806
  • 1
  • 17
  • 31
0

Faced the same issue and got it resolved by adding the below line in my zshrc. Based on your shell you can try adding in your rc file, for bash and zsh - bashrc/zshrc files present in your home location.

export NODE_PATH="/usr/local/lib/node_modules"

To directly add in zshrc file, run this command

echo 'export NODE_PATH="/usr/local/lib/node_modules"' >> ~/.zshrc
Lavish
  • 642
  • 7
  • 12
0

Alpine / Containerization

As mentioned elsewhere, the solution is to bake into your image,

NODE_PATH=/usr/local/lib/node_modules/

I've also opened up an issue upstream with npm apk so this gets set

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468