Cannot npm install -g without sudo changing prefix - node.js

I have done this: https://stackoverflow.com/a/21712034
now my npm config is the following:
npm config get prefix
/home/ranu/npm
But when I run npm install -g grunt-cli I get:
npm ERR! Error: EACCES, mkdir '/usr/lib/node_modules/grunt-cli'
npm ERR! { [Error: EACCES, mkdir '/usr/lib/node_modules/grunt-cli']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/lib/node_modules/grunt-cli',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/lib/node_modules/grunt-cli',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ '/usr/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23',
npm ERR! '/usr/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Linux 3.11.0-26-generic
npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "-g" "grunt-cli"
npm ERR! cwd /home/ranu
npm ERR! node -v v0.10.29
npm ERR! npm -v 1.4.14
npm ERR! path /usr/lib/node_modules/grunt-cli
npm ERR! fstream_path /usr/lib/node_modules/grunt-cli
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/lib/node_modules/grunt-cli'
npm ERR! fstream_stack /usr/lib/node_modules/npm/node_modules/fstream/lib/dir- writer.js:36:23
npm ERR! fstream_stack /usr/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/ranu/npm-debug.log
npm ERR! not ok code 0
Why is trying to install it on /usr/lib/node_modules/ if the prefix is set to /home/ranu/npm ?
UPDATE:
I could it make it work with the following command:
npm install --prefix="/home/ranu/npm" -g grunt-cli
There is any way to make it as a default?

Every time I get a new Machine, I run into this.
I'm citing this url fixing-npm-permissions because it's actually NPM's doc, but also because it was the only method that fixed my problem. Not the first search result but the simplest fix for me. I'm sure there are more robust fixes.
Rather than copying and pasting, I'm recommending you just go read it.
I followed step 1, and though according to them it should have worked. But, I ended up also using step 2 for the win.

You can use NVM (Node version manager) and you will never have to bother about sudo for npm commands

Related

npm install ember-cli fails on ubuntu trusty in vagrant virtualbox

I have not been able to successfully install ember-cli. I finally was able to run:
sudo npm install -g ember-cli
But when running:
user#host:~ ember new ember-app
I get:
/usr/bin/env: node: No such file or directory
I installed node.js from the repositories using sudo apt-get install nodejs and same with npm.
I have installed bower and tried to re-install npm. Here is the last error:
user#host:~ npm install -g ember-cli
npm http GET https://registry.npmjs.org/ember-cli
npm http 200 https://registry.npmjs.org/ember-cli
npm http GET https://registry.npmjs.org/ember-cli/-/ember-cli-0.2.7.tgz
npm http 200 https://registry.npmjs.org/ember-cli/-/ember-cli-0.2.7.tgz
npm ERR! Error: shasum check failed for /home/user/tmp/npm-14461-xfhNCJKF/1435776262780-0.6301117099355906/tmp.tgz
npm ERR! Expected: fefcd9fe7faf276a5a68c8bfcc92288c0f0678d5
npm ERR! Actual: c120ca4196f11da6e51f9c8ebb273439dcfee615
npm ERR! at /usr/lib/nodejs/sha/index.js:38:8
npm ERR! at ReadStream.<anonymous> (/usr/lib/nodejs/sha/index.js:85:7)
npm ERR! at ReadStream.EventEmitter.emit (events.js:117:20)
npm ERR! at _stream_readable.js:920:16
npm ERR! at process._tickCallback (node.js:415:13)
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! <npm-#googlegroups.com>
npm ERR! System Linux 3.13.0-55-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "-g" "ember-cli"
npm ERR! cwd /home/user
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/user/npm-debug.log
npm ERR! not ok code 0
For clarity, I am able to install other items using npm. Just not ember-cli.
UPDATE:
I am getting similar errors that I've received in previous executions of npm install -g ember-cli when run the following:
npm install -g latest-version
Received the following error:
npm http GET https://registry.npmjs.org/latest-version
npm http 304 https://registry.npmjs.org/latest-version
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/latest-version'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/latest-version']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/latest-version',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/latest-version',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ '/usr/lib/nodejs/fstream/lib/dir-writer.js:36:23',
npm ERR! '/usr/lib/nodejs/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Linux 3.13.0-55-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "--global" "latest-version"
npm ERR! cwd /home/user
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /usr/local/lib/node_modules/latest-version
npm ERR! fstream_path /usr/local/lib/node_modules/latest-version
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/latest-version'
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/dir-writer.js:36:23
npm ERR! fstream_stack /usr/lib/nodejs/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/user/npm-debug.log
npm ERR! not ok code 0
-
UPDATE:
"Fix" is in the comment below.
For your node issue, try the following command which will make it so your system recognizes your node.js directory as node:
sudo ln -s /usr/bin/nodejs /usr/bin/node
Regarding the issue with administrator access, simply run your commands with sudo at the beginning. Sudo is usually required in situations where modifications to the filesystem are necessary.

installing semantic-UI on ubuntu (terminal error)

I followed the instructions: http://learnsemantic.com/guide/expert.html
And I got to the point where I should run this command:
npm install -g gulp
I get the following result:
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Linux 3.19.0-16-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "-g" "gulp"
npm ERR! cwd /home/yannick
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.4.21
npm ERR! path /usr/local/lib/node_modules
npm ERR! fstream_path /usr/local/lib/node_modules/gulp
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules'
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:171:23
npm ERR! fstream_stack /usr/lib/nodejs/mkdirp/index.js:46:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/yannick/npm-debug.log
npm ERR! not ok code 0
Can anyone tell me what I am doing wrong?
Solved! The answer can be found here: https://github.com/joyent/node/issues/3911#issuecomment-8956154
:)
The key is line 3,
npm ERR! Please try running this command again as root/Administrator.
Try running the command as a sudo user or even better make sure that you can install npm modules globally without needing the sudo command. The guide I last used can be found is here: https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md

Cant install npm packages with out sudo

I am trying to install node-debugger and can't install with out sudo. Using sudo gets me some unexpected sideeffects, so want to install with out sudo. Following is the error trace
$ npm install -g node-inspector
npm http GET https://registry.npmjs.org/node-inspector
npm http 304 https://registry.npmjs.org/node-inspector
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/node-inspector'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/node-inspector']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/node-inspector',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/node-inspector',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Linux 3.8.0-35-generic
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "node-inspector"
npm ERR! cwd /home/santhosh/dev/scaleqa/mean_tut/mean_io_original/scaleqaApp
npm ERR! node -v v0.10.22
npm ERR! npm -v 1.3.14
npm ERR! path /usr/local/lib/node_modules/node-inspector
npm ERR! fstream_path /usr/local/lib/node_modules/node-inspector
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/node-inspector'
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/santhosh/dev/scaleqa/mean_tut/mean_io_original/scaleqaApp/npm-debug.log
npm ERR! not ok code 0
You have to use sudo; this is because when you install globally, NPM creates links from your bin folder(s) to scripts that run node and the specified scripts from the packages (making node-inspector somescript.js really run node /path/to/inspector/node_inspector.js somescript.js.
Those bin folders are protected to those who are not super users, therefore sudo must be used to elevate your access level and allow npm to write to those directories.
You are being asked for sudo because you are installing the npm module globally i.e you are using -g and they are stored in bin which can be accessed by sudo user !
if you do not use -g i.e you install it locally it wont ask for sudo permission!

Create a full directory structure/skeleton through the terminal - expressJS/nodeJS

I have seen some tips on the internet on how to create the full directory structure for a node project with a command line with expressJS.
Here are some examples:
express express-skeleton << http://quickleft.com/blog/getting-started-with-express-in-node
./node_modules/.bin/express -t ejs << http://naholyr.fr/2011/08/ecrire-service-rest-nodejs-express-partie-1/
--l << http://www.techrepublic.com/article/build-nodejs-web-applications-with-express/
But none of those work for me. I have install express and update it to have it globally installed with npm update express -g but my terminal doesn't even recognize the command line express.
How can I build a web app skeleton easily with the command line? Thanks a lot
EDIT: when running the npm install -g express command line, I get this error message:
npm http GET https://registry.npmjs.org/express
npm http 200 https://registry.npmjs.org/express
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/express'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/express']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/express',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/express',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Darwin 13.0.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "express"
npm ERR! cwd /Users/arnauddrizard/Documents/Dev/RestAPI
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! path /usr/local/lib/node_modules/express
npm ERR! fstream_path /usr/local/lib/node_modules/express
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/express'
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/arnauddrizard/Documents/Dev/RestAPI/npm-debug.log
npm ERR! not ok code 0
npm unable to create folder inside your application folder.
Give sufficient permissons to your folder, I hope that will fix your issue.
What output do you get when you run npm install -g express? The problem is most likely that npm install -g is not installing to somewhere that's in your PATH.

How can I install the grunt-cli without getting errors?

Inspired by Chris Coyier's post, I decided I'd give grunt a go. But I'm having big problems getting set up.
First, I installed Node.
Then I added a package.json file to my project root, including this:
{
"name": "example-project",
"version": "0.1.0",
"devDependencies": {
"grunt": "~0.4.1"
}
}
Then I ran npm install
Finally, I ran npm install -g grunt-cli
which came back with a ton of errors:
npm http GET https://registry.npmjs.org/grunt-cli
npm http 304 https://registry.npmjs.org/grunt-cli
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/grunt-cli',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/grunt-cli',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Darwin 11.4.2
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "grunt-cli"
npm ERR! cwd /Users/bryce/repo
npm ERR! node -v v0.10.23
npm ERR! npm -v 1.3.17
npm ERR! path /usr/local/lib/node_modules/grunt-cli
npm ERR! fstream_path /usr/local/lib/node_modules/grunt-cli
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/bryce/repo/npm-debug.log
npm ERR! not ok code 0
Bryce:repo bryce$ npm install -g grunt-cli
npm http GET https://registry.npmjs.org/grunt-cli
npm http 304 https://registry.npmjs.org/grunt-cli
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/grunt-cli',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/grunt-cli',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Darwin 11.4.2
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "grunt-cli"
npm ERR! cwd /Users/bryce/repo
npm ERR! node -v v0.10.23
npm ERR! npm -v 1.3.17
npm ERR! path /usr/local/lib/node_modules/grunt-cli
npm ERR! fstream_path /usr/local/lib/node_modules/grunt-cli
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/grunt-cli'
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/bryce/repo/npm-debug.log
npm ERR! not ok code 0
I assume the above errors are the reason that when I tried to run grunt, I simply get -bash: grunt: command not found
Anyone out there know what's happening?
All it took was adding 'sudo', since I didn't have permission to install the grunt-cli by default. Once I ran the following command and entered my password, it worked as expected.
sudo npm install -g grunt-cli
Hope this helps someone!
EDIT:
The article quoted below is quite old and refers to pre-v0.3 Yikes! Currently, if npm is run with sudo rights it will downgrade to the nobody user before executing commands.
According to the maintainer of npm, installing packages with sudo is considered bad practice because you are allowing that package to have complete control of your system and you can't and SHOULDN'T trust these packages with root access.
http://howtonode.org/introduction-to-npm
Since you should not install using sudo - you will do this before installing packages and no longer will hit permission errors:
sudo chown -R $USER /usr/local
source: https://howtonode.org/introduction-to-npm
I would recommend installing grunt within your project (instead of using the -g option).
For example, to install the Grunt command line tools use:
cd ~/path-to-my-project
npm install grunt-cli --save-dev
If you want to run grunt then you use:
cd ~/path-to-my-project
./node_modules/./bin/grunt
By installing grunt-cli within your project, you'll have full control over which version gets installed and you don't have to rely on inconsistent PATH variables.
I was having this issue as well, and after reading through the output a little closer i realized that my installed version of node.js was not compatible with the current version of grunt-cli.
Updated node.js problem solved.

Resources