23

I am starting my first Node.js server. I am attempting to follow a tutorial in Node.js.

Having installed Node.js and executed npm install, the next instruction is to start the server by running node server.js which generates the following error:

node.js:201
    throw e; // process.nextTick error, or 'error' event on first tick
          ^
Error: Cannot find module 'express'
    at Function._resolveFilename (module.js:332:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:354:17)
    at require (module.js:370:17)
    at Object.<anonymous> (/home/sisko/Desktop/ExerciseFiles/01/03/start/server.js:1:77)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:32)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)
sisko@VirtualBox:~/Desktop/ExerciseFiles/01/03/start$ 

It seemed it needed a module called express so I looked around and found the npm install express command which should fix the issue. However, I get the following error:

sisko@VirtualBox:~/Desktop/ExerciseFiles/01/03/start$ npm install express

npm http GET https://registry.npmjs.org/express

npm ERR! Error: failed to fetch from registry: express
npm ERR!     at /usr/share/npm/lib/utils/npm-registry-client/get.js:139:12
npm ERR!     at cb (/usr/share/npm/lib/utils/npm-registry-client/request.js:31:9)
npm ERR!     at Request._callback (/usr/share/npm/lib/utils/npm-registry-client/request.js:136:18)
npm ERR!     at Request.callback (/usr/lib/nodejs/request/main.js:119:22)
npm ERR!     at Request.<anonymous> (/usr/lib/nodejs/request/main.js:212:58)
npm ERR!     at Request.emit (events.js:88:20)
npm ERR!     at ClientRequest.<anonymous> (/usr/lib/nodejs/request/main.js:412:12)
npm ERR!     at ClientRequest.emit (events.js:67:17)
npm ERR!     at HTTPParser.onIncoming (http.js:1261:11)
npm ERR!     at HTTPParser.onHeadersComplete (http.js:102:31)
npm ERR! You may report this log at:
npm ERR!     <http://bugs.debian.org/npm>
npm ERR! or use
npm ERR!     reportbug --attach /home/sisko/Desktop/ExerciseFiles/01/03/start/npm-debug.log npm
npm ERR! 
npm ERR! System Linux 3.2.0-52-generic
npm ERR! command "node" "/usr/bin/npm" "install" "express"
npm ERR! cwd /home/sisko/Desktop/ExerciseFiles/01/03/start
npm ERR! node -v v0.6.12
npm ERR! npm -v 1.1.4
npm ERR! message failed to fetch from registry: express
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /home/sisko/Desktop/ExerciseFiles/01/03/start/npm-debug.log
npm not ok
halfer
  • 19,824
  • 17
  • 99
  • 186
sisko
  • 9,604
  • 20
  • 67
  • 139

2 Answers2

91

Seems like it's a SSL certificate issue with the repository, because it worked with HTTP but not HTTPS.

For troubleshooting only

To troubleshoot, on a sandbox where you're okay running untrusted code, you can confirm this by trying the repository under HTTP this insecure hack: running npm config set registry http://registry.npmjs.org/ (or whaterver repo) then npm install express. If that worked when the HTTPS doesn't, then it's probably an SSL issue.

Please do not use this insecure config as a fix. It's not a fix. Installing from HTTP opens you up to running code from a malicious source.

The fix is to update node/npm versions and/or fix the SSL.

bakkal
  • 54,350
  • 12
  • 131
  • 107
  • 4
    This worked for me when I had the same problem installing a different package install, the clevertech-cli. It isn't just for express. – Tony Adams Apr 25 '14 at 00:48
  • 1
    Could I know the reason why npm is configured, by default, with registry equals to https://registry.npmjs.org/ on debian and why it doesn't work for install package like express ? And it works for the same URL without ssl ? – AntiClimacus May 01 '14 at 12:38
  • 1
    worked for me as well to fix this error after installing npm package in ubuntu 12.04: "npm ERR! Error: failed to fetch from registry: canvas/1.0.4" ! Thank you – Matthias123 Jul 06 '14 at 16:22
  • 1
    This seems to work, but is not a fix, IMHO. It allows a DNS poisoning attack to run arbitrary code as superuser. A workaround might be to install npm from source instead offrom Ubuntu 12.04 repository. – dcorking Jul 09 '14 at 13:39
35

This helps as an alternative solution to accepted answer:

npm config set strict-ssl false

The problem with npm is that the default registry ssl certificate is not recognized properly.

hegemon
  • 6,614
  • 2
  • 32
  • 30