Which combination of global/local sudo/notsudo for which node.js npm install package?

I’d like to install packages like these on MacOSX, but interested in Windows or Linux versions too:

npm install express
npm install swig
npm install mongodb
npm install consolidate
npm install http-auth

If I call npm without sudo, some of them will fail:

Error: EACCES, open '/Users/me/.npm/cookie/0.1.2/package.tgz'
npm ERR!  { [Error: EACCES, open '/Users/me/.npm/cookie/0.1.2/package.tgz']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/Users/me/.npm/cookie/0.1.2/package.tgz',
npm ERR!   parent: 'express' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator

If I install with sudo, the files end up wherever my current directory is, and there under node_modules.

If I use the -g command, I get the access problem.

If I use the -g with sudo, it installs, but the node app cannot find the packages:

module.js:340
    throw err;
          ^
Error: Cannot find module 'express'

Am I missing something important? What is the correct way to install these packages? (I was told, that installing globally should be preferred, so that all apps are always linked to the most up-to-date package version, so if that makes a difference, please let me know)

Answer

Node.js will traverse folders upwards until it finds your package, so you can install all in your home folder:

cd ~
npm install express

Attribution
Source : Link , Question Author : massimo , Answer Author : user235456

Leave a Comment