Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run NPM without sudo?

I've been reading posts, trying to figure out how to run npm install without sudo. I also came across the following article but I think it's for an older version of NPM. I done the following which produced less errors, but still throw errors at the end:

sudo chown -R $USER /usr/local

The only way I can get no errors is to run sudo npm install but I read this is not a good idea.

Below is the out I see after running it. My guess is permissions but I don't really know what folders I should chown (if at all). Sorry about the huge amount of data, I don't really know what is relevant. I couldn't see anything helpful in the log either (/var/www/budget/npm-debug.log) although it is huge. Is there anything I still need to chown?

$ npm install
npm WARN package.json [email protected] No description
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No README data
npm http GET https://registry.npmjs.org/grunt-contrib-watch
npm http GET https://registry.npmjs.org/grunt-contrib-less
.
.
.
npm http 304 https://registry.npmjs.org/hawk/1.1.1
npm http GET https://registry.npmjs.org/delayed-stream/0.0.5
npm ERR! Error: ENOENT, chmod '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/bl/test/test.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/bl/test/test.js
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/bl/test/test.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! fstream_finish_call chmod
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:305:19
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! error rolling back Error: ENOTEMPTY, rmdir '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/node-uuid'
npm ERR! error rolling back  [email protected] { [Error: ENOTEMPTY, rmdir '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/node-uuid']
npm ERR! error rolling back   errno: 53,
npm ERR! error rolling back   code: 'ENOTEMPTY',
npm ERR! error rolling back   path: '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/node-uuid' }
npm WARN optional dep failed, continuing [email protected]
npm ERR! Error: ENOENT, lstat '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/qs/lib/utils.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/qs/lib/utils.js
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/qs/lib/utils.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! Error: ENOENT, lstat '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/http-signature/lib/util.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/http-signature/lib/util.js
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/http-signature/lib/util.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm http 304 https://registry.npmjs.org/delayed-stream/0.0.5
npm ERR! Error: ENOENT, lstat '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/tough-cookie/test.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/tough-cookie/test.js
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/tough-cookie/test.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
[email protected] node_modules/grunt-cache-breaker
└── [email protected] ([email protected])

[email protected] node_modules/grunt-contrib-watch
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected])

[email protected] node_modules/grunt
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected])
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected])

[email protected] node_modules/grunt-contrib-cssmin
├── [email protected] ([email protected], [email protected], [email protected])
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected])

[email protected] node_modules/grunt-contrib-less
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected], [email protected])
npm ERR! Error: Callback called more than once.
npm ERR!     at errorHandler (/usr/share/npm/lib/utils/error-handler.js:80:16)
npm ERR!     at save (/usr/share/npm/lib/install.js:329:12)
npm ERR!     at /usr/share/npm/lib/install.js:98:7
npm ERR!     at /usr/share/npm/lib/install.js:188:5
npm ERR!     at /usr/share/npm/node_modules/read-installed/read-installed.js:119:5
npm ERR!     at /usr/share/npm/node_modules/read-installed/read-installed.js:233:14
npm ERR!     at cb (/usr/lib/nodejs/slide/lib/async-map.js:48:11)
npm ERR!     at /usr/share/npm/node_modules/read-installed/read-installed.js:233:14
npm ERR!     at cb (/usr/lib/nodejs/slide/lib/async-map.js:48:11)
npm ERR!     at /usr/share/npm/node_modules/read-installed/read-installed.js:233:14
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! Error: ENOENT, lstat '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/mime-types/lib/mime.json'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/mime-types/lib/mime.json
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/mime-types/lib/mime.json
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! Error: ENOENT, lstat '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/hawk/images/logo.png'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <[email protected]>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/hawk/images/logo.png
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/hawk/images/logo.png
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /var/www/budget/npm-debug.log
npm ERR! not ok code 0
like image 616
Martyn Avatar asked Jan 03 '15 13:01

Martyn


People also ask

How do I run npm in command prompt?

Configuring the StepAdd the Run npm command Step to your Workflow preceding any build Step. Set the Working directory. Set the command you want npm to execute, for example install to run npm install in the The npm command with arguments to run input.

Can npm run without node?

No, because npm is a program written in JavaScript, and without Node rather hard to run such programs on the server side (outside the browser). One might be providing a "compiled" node program using something like zeit pkg which packages the node.


2 Answers

If you want to run commands like npm install -g protractor without using sudo you need to

  • Configure NPM to copy package files to, and execute from a location that doesnt require sudo permissions (I believe the only place in Ubuntu which allows this by default is /home/user-name/)
    .
  • Update the $PATH variable that BASH looks to when determining what scripts/programs are available to run from a command prompt

First need to establish where node is currently configured to install global packages

Based on NPM official config docs - as well as various other sources
run:

$ npm config get prefix

or

$ npm config list

or (to show all config defaults)

$ npm config ls -l

NPM prefix value determines where node packages are installed.

In other words, if the result of running npm config list looks like this:

; userconfig /home/michael/.npmrc
prefix = "/home/michael/npm"

It means

  • you have a user level config /home/michael/.npmrc that NPM will parse with higher priority than the global/system config file
  • that when you specify a global install npm install -g protractor protractor files will be installed into /home/michael/npm
Now make the changes!

set the prefix directly by using :

npm config set prefix ~/npm

Getting the NPM package script files into BASH's path

append (edit) the .bashrc file with:

export PATH="$PATH:$HOME/npm/bin"

and run

$ source .bashrc

to re-parse the .bashrc script

like image 169
Michael Coleman Avatar answered Oct 10 '22 21:10

Michael Coleman


From the logs it seems you have your application stored in /var/www/budget, you cwd into this directory and run npm install. However, this path is most commonly owned by the Apache service user (apache or httpd, not sure which one exactly, it might also depend on the software you use).

I do not recommend taking ownership of this folder to your user account. However, it might be quite ok to put your user account into the apache user group and change the permissions so that the group has read/write access (if it already has not):

(make sure that the group is really apache before executing, lest you would give r/w permissions to some other group)

sudo chmod -R g+r,g+w /var/www

In general, you have allowed yourself to install npm modules globally (into /usr/local, with the -g flag), but your application does not reside within that directory, so you must decide the best course of action to amend the permission settings for that particular application's path.

like image 41
Robert Rossmann Avatar answered Oct 10 '22 19:10

Robert Rossmann