[node.js] NPM global install "cannot find module"

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 <[email protected]>",
  "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.

This question is related to node.js npm

The answer is


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.


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


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


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.


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


add this to beginning of prog(mac):

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


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)


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


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

NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
export NODE_PATH="$NODE_PATH"

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


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


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.


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

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.


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

npm link wisp

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


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 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?