0

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)

massimo
  • 1
  • 2

2 Answers2

0

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

cd ~
npm install express
0

From the npm help 5 npm-foldersman page:

  • Local install (default):
    puts stuff in ./node_modules of the current package root.

  • Global install (with -g):
    puts stuff in /usr/local or wherever node is installed.

  • Install it locally if you're going to require() it.

  • Install it globally if you're going to run it on the command line.

  • If you need both, then install it in both places, or use npm link.

The only time I used sudo was the install of nw because I wanted it to be linked in /usr/bin. After the invoke with sudo I ran into similar Errors, because root as owned my ~/.npm/_locks directory.

Every install ran into Error, until I chown ~/.npm and ~/.npm/_locks back to my user.

Follow the instruction of man page and check ~/.npm everytime you sudo.

sebix
  • 4,175
  • 2
  • 25
  • 45