34

This is what I get when I try to install express for node.js

npm install express

module.js:340
    throw err;
          ^
Error: Cannot find module 'graceful-fs'
    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/share/npm/lib/utils/ini.js:32:10)
    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.require (module.js:362:17)

I even tried others and still got the same error. I did some digging and i did find 'gracefui-fs' on my server.

I am running ubuntu 12.04 LTS on an amazon ec2 server. Node installed with no errors, and npm still didn't work. I also un installed and re installed npm using

apt-get

as well as from source. During installation I received no errors either.

Collin McGuire
  • 714
  • 3
  • 8
  • 14
  • I'm having the same problem :( http://stackoverflow.com/questions/16973604/npm-cannot-find-module-tobi-cookie-npm-unable-to-install-express-node-js – Katie Jun 06 '13 at 23:03
  • I've been stuck on this issue for the last 5 hours :( It only happens when deploying to my Bitbucket Pipelines – Brady Edgar Oct 02 '21 at 05:17

18 Answers18

37

Had the same problem. Don't download npm from aptitude or apt-get.

Instead try:

git clone --depth=1 git://github.com/npm/cli.git
cd cli/scripts
chmod +x install.sh
sudo ./install.sh

saw it here: NPM can't install appjs. Error: Cannot find module 'graceful-fs'

Community
  • 1
  • 1
Giuliani
  • 395
  • 3
  • 3
  • Used this solution to install latest version of node.js on Ubuntu – Pradeep Oct 24 '13 at 06:49
  • This worked for me on OS X 10.9. I had originally updated node only, but it required this fix to straighten out npm. – sambecker Aug 02 '14 at 03:31
  • go to your user's directory, delete `npm` file, and then go to cmd and reinstall npm – Kyle Feb 29 '16 at 18:12
  • Why are you installing from an unofficial repo instead of https://github.com/npm/npm? – Cerin Mar 29 '17 at 15:30
  • 2
    it doesn't help – Dmitry Grinko May 14 '18 at 01:11
  • 2
    does nothing - now i have some weird thing installed on my computer – user1709076 Dec 18 '18 at 11:37
  • many problems with this "top" answer... (1) never execute a downloaded script without verifying its integrity / content / source signatures... because of this any apt-based solutions are certainly better even if they don't actually work, (2) "git" protocol (git://) is deprecated and insecure and leaves this command open to MITM attacks which when combined with the previous issue can literally take over your machine (aka trojan by DNS poisoning or something), (3) installing as "root" via the sudo wrapper will mess up your permissions by installing everything as root:root instead of your user – obimod Jul 28 '23 at 15:45
20

I had the same problem.

sudo npm install 

solved it for me.

Sirko
  • 72,589
  • 19
  • 149
  • 183
Max
  • 249
  • 2
  • 3
  • Men this was so simple but yet powerful thanks for this reply! – Jorge Vicente Mendoza Jan 28 '16 at 20:18
  • 6
    `sudo npm install` generally causes more issues than it's worth. If this even solves the problem at all then it usually means that you have broken permissions as it is, and you likely have unexpected build problems all over the place. TL;DR: don't do this. Ever. It's bad, and often breaks things that aren't already broken. – aggregate1166877 Jan 12 '18 at 16:12
15

I was also using Ubuntu 12.04, and was having this same issue (though I don't think it's specifically Ubuntu's fault).

grimbo@grimbo-ubuntu-12:~/git/SyntaxHighlighter$ npm

module.js:340
    throw err;
          ^
Error: Cannot find module 'graceful-fs'
    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/share/npm/lib/utils/ini.js:32:10)
    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.require (module.js:362:17)

So i tried to work out where npm was running from:

grimbo@grimbo-ubuntu-12:~/git/SyntaxHighlighter$ sudo find / -name npm
/etc/bash_completion.d/npm
/usr/bin/npm
/usr/local/bin/npm
/usr/local/n/versions/0.8.7/bin/npm
/usr/local/n/versions/0.8.7/lib/node_modules/npm
/usr/local/n/versions/0.8.7/lib/node_modules/npm/bin/npm
/usr/share/lintian/overrides/npm
/usr/share/doc/npm
/usr/share/npm
/usr/share/doc-base/npm
/usr/lib/nodejs/npm
/var/lib/doc-base/documents/npm

For reference, these were a couple of the links:

/usr/bin/npm -> ../share/npm/bin/npm-cli.js*
/usr/local/n/versions/0.8.7/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js*

Confirmation that it was "/usr/bin/npm" that was being used:

grimbo@grimbo-ubuntu-12:~/git/SyntaxHighlighter$ which npm
/usr/bin/npm

So I tried to call the latest one specifically, which seemed to do the trick:

grimbo@grimbo-ubuntu-12:~/git/SyntaxHighlighter$ /usr/local/n/versions/0.8.7/bin/npm install
npm WARN package.json SyntaxHighlighter@3.0.83 No README.md file found!
npm http GET https://registry.npmjs.org/uglify-js
npm http GET https://registry.npmjs.org/less
npm http GET https://registry.npmjs.org/jake
npm http GET https://registry.npmjs.org/minimatch
npm http 200 https://registry.npmjs.org/uglify-js
npm http GET https://registry.npmjs.org/uglify-js/-/uglify-js-1.3.3.tgz
npm http 200 https://registry.npmjs.org/less
npm http GET https://registry.npmjs.org/less/-/less-1.3.0.tgz
npm http 200 https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/minimatch/-/minimatch-0.2.6.tgz
npm http 200 https://registry.npmjs.org/less/-/less-1.3.0.tgz
npm http 200 https://registry.npmjs.org/uglify-js/-/uglify-js-1.3.3.tgz
npm http 200 https://registry.npmjs.org/minimatch/-/minimatch-0.2.6.tgz
npm http 200 https://registry.npmjs.org/jake
npm http GET https://registry.npmjs.org/jake/-/jake-0.3.14.tgz
npm http 200 https://registry.npmjs.org/jake/-/jake-0.3.14.tgz
npm http GET https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/utilities
npm http GET https://registry.npmjs.org/minimatch
npm http 304 https://registry.npmjs.org/minimatch
npm http GET https://registry.npmjs.org/minimatch/-/minimatch-0.0.5.tgz
npm http 200 https://registry.npmjs.org/utilities
npm http GET https://registry.npmjs.org/utilities/-/utilities-0.0.11.tgz
npm http 200 https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/lru-cache/-/lru-cache-2.0.1.tgz
npm http 200 https://registry.npmjs.org/minimatch/-/minimatch-0.0.5.tgz
npm http 200 https://registry.npmjs.org/utilities/-/utilities-0.0.11.tgz
npm http 200 https://registry.npmjs.org/lru-cache/-/lru-cache-2.0.1.tgz
npm http GET https://registry.npmjs.org/lru-cache
npm http 304 https://registry.npmjs.org/lru-cache
npm http GET https://registry.npmjs.org/lru-cache/-/lru-cache-1.0.6.tgz
npm http 200 https://registry.npmjs.org/lru-cache/-/lru-cache-1.0.6.tgz
npm WARN prefer global jake@0.3.14 should be installed with -g
uglify-js@1.3.3 node_modules/uglify-js

less@1.3.0 node_modules/less

minimatch@0.2.6 node_modules/minimatch
+-- lru-cache@2.0.1

jake@0.3.14 node_modules/jake
+-- utilities@0.0.11
+-- minimatch@0.0.5 (lru-cache@1.0.6)

Ultimately, it looks like maybe because I've had other versions of node and npm installed in the past, it just got confused.

Paul Grime
  • 14,970
  • 4
  • 36
  • 58
13

So after installing node_js and npm, I was having the same problem

my-app# apt-get install mlocate || yum install mlocate
...
my-app$ locate graceful-fs
/usr/lib/nodejs/graceful-fs
... some other junk we don't care about ...
my-app$ export NODE_PATH=/usr/lib/nodejs
my-app$ npm update
... it works ...
user1742529
  • 260
  • 4
  • 16
Kevin Meyer
  • 2,816
  • 4
  • 21
  • 33
5

Just for my two cents. I install with

sudo npm install -g graceful-fs

This install graceful-fs globally and it solved my issue.

Prut Udomwattawee
  • 665
  • 1
  • 9
  • 13
1

By default Node looks for modules within your project in ../CollinsProject/node_modules.

Try moving graceful-fs to this folder and it should work.

If you want to install the module to be used globally (across other projects), then move the graceful-fs folder to /usr/local/lib/node_modules.

joshhua
  • 31
  • 2
  • The module is in the global folder and it still gave me an error. I deleted the Ubuntu 12.04 instance and installed Ubuntu 11 and it worked fine, no errors. My machine at home is running Ubuntu 12.04 and it works fine. – Collin McGuire Jul 11 '12 at 00:17
1

Had similar problem on Ubuntu while installing phonegap, fixed it with

sudo npm install graceful-fs
Artjom Kurapov
  • 6,115
  • 4
  • 32
  • 42
1

I use Windows; similar should work in Mac and Linux

npm in cordova wrong and cordova uses private npm

go to C:\Users\sssca_000\AppData\Roaming\npm\node_modules\cordova\node_modules Rename npm to npm_XXX

Go to C:\Users\sssca_000\AppData\Roaming\npm\node_modules This is where real npm resides Copy npm directory from here to C:\Users\sssca_000\AppData\Roaming\npm\node_modules\cordova\node_modules

It worked fine for me!

Jayanta
  • 135
  • 4
  • I don't think this is helpful; it doesn't diagnose the problem at all, and it doesn't suggest a solution. (At best, it's a comment.) – David Manheim Jan 20 '16 at 05:03
1

I had Mint Node.js 8.x version but have upgraded via n command to 11.x. The problem was that n/npm only replace binary or link in /usr/local/bin directory, but not in /usr/bin directory. So you should replace /usr/bin/node-gyp with /usr/local/bin/node-gyp content:

sudo cp -f -s /usr/local/bin/node-gyp /usr/bin/node-gyp

Volodymyr Bryliant
  • 2,352
  • 1
  • 14
  • 9
1

i did that and it finally worked, have to install by npm not apt

sudo apt-get purge --auto-remove node-gyp
npm install -g node-gyp
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
remon78eg
  • 61
  • 1
  • 5
0

I'm a self confessed muppet when it comes to linux but rfumbled my way onto a solution. I'm running ubuntu 12.04 and was having the same issue. Tried this and it worked for me, maybe a better man than me can explain why and might even work for you:

sudo npm install socket.io

I had previously installed npm and node from git and all appeared to be working until I tried to install less and got the same error as OP.

SwiftD
  • 5,769
  • 6
  • 43
  • 67
0

The same also happens on Windows if you install in a custom directory. You have to uninstall and install again under default path.

Nux
  • 9,276
  • 5
  • 59
  • 72
0

I came across the same problem and after searching online, the only thing that worked for me was to reinstall nodejs, nodejs-dev and npm. I followed the directions from this site: https://github.com/joyent/node/wiki/installing-node.js-via-package-manager

Christie
  • 233
  • 2
  • 9
0

The solution for me on windows 10 after installing the last stable version of node

  • Removing the current npm directory in the main directory nodejs/node_modules
  • Cloning the last build of npm https://github.com/npm/npm.git into the nodejs/node_modules dir
henkieee
  • 1,123
  • 1
  • 8
  • 10
0

In my CENTOS 7 case, i had to delete old version lib files.

rm -rf /usr/lib/node_modules/*

After that a yum reinstall nodejs solved!

LeoPucciBr
  • 151
  • 1
  • 10
0

I have fixed the problem on linux ubuntu in this way:

1. I removed nvm by command : sudo apt-get remove nvm

then:

git clone https://github.com/creationix/nvm.git ~/nvm
//For start node you have to run this command every time if you do not add in .profile etc
. ~/nvm/nvm.sh
// Now Install latest stable version of NodeJS
nvm install v5.7.1
// Check installed nodejs version
nvm ls
//You can set which nodejs version you want to use
nvm use v5.7
Ashfaq Muhammad
  • 780
  • 5
  • 10
0
Error: Cannot find module 'graceful-fs'

Require stack:

  • C:\Users\Izhar\AppData\Local\npm-cache_npx\c67e74de0542c87c\node_modules\fs-extra\lib\fs\index.js

copy the error showing path upto node_modules folder

ex : C:\Users\Izhar\AppData\Local\npm-cache\_npx\c67e74de0542c87c\node_modules

and cd over to the path

cd C:\Users\Izhar\AppData\Local\npm-cache\_npx\c67e74de0542c87c\node_modules

and then install graceful-fs with

npm i graceful-fs
Izhar Ul Haque
  • 116
  • 1
  • 5
0

For those like me who have a custom NPM setup (e.g. to separate NPM/Node by project instead of by version) and thus create things like NPM_CONFIG_PREFIX and other environment variables:

npx -c '<command>'

This executes NPX commands while respecting NPM environment variables. This solved my graceful-fs error msg by telling the installer where the global NPM directories are via env vars.

source: https://brianchildress.co/environment-variables-using-npx/

Unrelated note on my specific setup, I use python's virtualenv (w/ virtualenv wrapper) and nodeenv (installs node w/ virtualenv)... this has been a holy grail setup for me for well over a decade now.

obimod
  • 797
  • 10
  • 26