npm command - sudo or not? - macos

Currently I always run sudo npm install <package-name> but as I understand it's not correct.
I want to have opportunity not to use it as root/Administrator.
I followed some advice and used this command sudo chown -R <username> ~/.npm but it won't work...
for example, it's an output of my npm install jade
...
npm http 200 https://registry.npmjs.org/amdefine
npm http GET https://registry.npmjs.org/amdefine/-/amdefine-0.0.5.tgz
npm http 200 https://registry.npmjs.org/amdefine/-/amdefine-0.0.5.tgz
npm ERR! Error: EACCES, symlink '../jade/bin/jade'
npm ERR! { [Error: EACCES, symlink '../jade/bin/jade'] errno: 3, code: 'EACCES', path: '../jade/bin/jade' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
as you see download started successfully but then failed..
I'm wondering what is the best way to disallow sudo on npm?

It's possible (and advisable) to npm install -g node modules without sudo.
Check the permission of your /usr/local/share/npm/bin folder. I had installed node and npm through brew (without sudo) and that particular folder ended up being owned by root.
This fixed it for once and for all:
$ sudo chown $(whoami) /usr/local/share/npm/bin
(As for disallowing sudo with npm: you'd have to tweak npm for that. Your own node code could make use of https://npmjs.org/package/sudo-block, npm install sudo-block)
EDIT: even though this works, I no longer use -g. Instead use prefix (see next answer), or better yet use NIX https://unix.stackexchange.com/a/381797 (even on OSX)

In my opinion is the cleanest way to specify the npm prefix:
npm config set prefix ~/.node_modules
And then to add the following to you .bash_profile
export PATH=$HOME/.node_modules/bin:$PATH
Now the packages will install into your user directory and no permissions will be harmend.
EDIT: If you can't install yeoman, create a bash file in one of your PATH directories named yodoctor with the following contents
#!/bin/bash
yo doctor
Make the file executable with
chmod +x yodoctor
And now you should be able to install yeoman.

You can also do:
sudo chown -R $USER /usr/local
and recursively change the files to your current user.

I have found this to be a better solution
sudo chown -R $USER /Users/$USER
This will just change the owner of your user to you and npm should be installed under your user on OS X.
Everything that I have been reading says sudo for npm installs is bad and I would have to agree with them as you open yourself up to malicious scripts.

The two solutions offered here are are not something I would recommend because they are brute force solutions. Instead, I recommend reading One does not simply sudo npm

Related

how to update npm on macOS

For reasons unknown to me, I haven't been able to update to the latest version of npm on macOS (it works fine on Windows). It displays no error, only 'updated 1 package'.
Using Node.js 8.11.1
node -v
v8.11.1
What version of npm do I have?
$ npm -v
5.6.0
I tried this...
$ npm i -g npm
+ npm#5.8.0
updated 1 package in 7.37s
And it fails to update.
$ npm -v
5.6.0
Where is npm?
$ which npm
/usr/local/bin/npm
So I try brew...
brew install npm
And it fails...
$ npm -v
5.6.0
*And then I tried this... *
npm install npm#latest -g
+ npm#5.8.0
updated 1 package in 7.618s
And it fails...
npm -v
5.6.0
With sudo:
sudo npm i -g npm
+ npm#5.8.0
updated 1 package in 7.794s
And it fails...
npm -v
5.6.0
This also fails...
sudo npm install npm#latest -g
I followed the directions found on this Q&A, completely removing npm and node from my system and reinstalling them from scratch, and it also fails to update.
Screenshot, per request:
Close the terminal, and then re-open the terminal and running:
$ npm -v
5.6.0
sudo twice:
$ sudo npm i -g npm
+ npm#5.8.0
updated 1 package in 7.478s
$ sudo npm i -g npm
+ npm#5.8.0
updated 1 package in 7.434s
Also fails:
$ npm -v
5.6.0
What did I miss? What's going on here?
This works on my mac.
Based on docs https://docs.npmjs.com/troubleshooting/try-the-latest-stable-version-of-npm :
npm install -g npm#latest
There is a note stated on the docs that depends on your installation method, you might addd some sudo.
Upgrading on *nix (OSX, Linux, etc.)
(You may need to prefix these commands with sudo, especially on Linux,
or OS X if you installed Node using its default installer.)
npm install -g npm#latest
works fine!!
and you can also replace the latest for specific versions
like
npm install -g npm#5.6.0
I hope it will help!!!
Perhaps you have already solved this, but here is what I found when I had exactly this issue. I had 2 versions of npm installed.
I verified this as follows:
grep \"version\" ~/.npm-packages/lib/node_modules/npm/package.json
"version": "6.2.0"
grep \"version\" /usr/local/lib/node_modules/npm/package.json
"version": "5.6.0",
I worked around the issue by updating the path in my bash profile, but would like to know why (how) I ended up with 2 versions. Here is the update:
tail -2 ~/.bash_profile
NPM_PACKAGES="${HOME}/.npm-packages"
PATH="$NPM_PACKAGES/bin:$PATH"
In my case, none of the previous answers worked. For me, a working solution was a simple, five-step process.
Make sure (the old version of) npm is installed.
npm -v
If npm is not installed, then install it on the Mac with Node.js.
Globally installed the desired version of npm.
npm install -g npm#latest
This command uses the old version of npm (installed by Node), to globally install the latest version of npm at ~/.npm-global/. Once installed, close and open a new terminal shell.
Remove the old version of npm installed by Node
rm -r /usr/local/lib/node_modules/npm/
Sometimes this doesn't work, so I had to go into finder to delete the /npm/ folder.
Make sure to set the correct path variable.
echo $PATH
If ~/.npm-global/bin does not show up between the colons, then update the path variable. Open up ~/.zshrc in a text editor and add the following line. If you don't use zsh, open the profile for your corresponding shell (i.e. ~/.bash_profile)
export PATH=$PATH:$HOME/.npm-global/bin
Save your changes and close the text editor.
Close and reopen the terminal shell and run npm -v to check that npm is correctly on the latest version.
The reason for updating the path variable is because the npm cli suggests you update npm with npm install -g npm which will install npm at a different location than where Node installs npm originally.
In my case, only the following has helped:
sudo rm -rf /usr/local/lib/node_modules
sudo rm -rf ~/.npm
brew uninstall --ignore-dependencies node
brew install node
EDIT NOV 21: These days, I bypass brew entirely and use "n":
https://www.npmjs.com/package/n
And so I can change between versions too.
I faced the same problem.
You might have already installed the npm version, and now it is time to point the new npm version install. You can do that by following below steps.
sudo nano /usr/local/lib/node_modules/npm/package.json
change "version" : "5.6.0" to "verison": "5.8.0"
In my case, I'm using nvm to manage different versions of node. In order to upgrade npm version, I have to
1 - Install the latest version of npm by navigating to your current
version of node
cd ~/.nvm/versions/node/v10.9.0
npm install npm
or you can probably use
nvm install-latest-npm
2 - Edit $PATH to point to your current version of node
NPM_PACKAGES="${HOME}/.nvm/versions/node/v10.9.0"
PATH="$NPM_PACKAGES/bin:$PATH"
On my macOS the homebrew-installed npm did not want to upgrade due to pre-existing link file. I ran the install with --force flag and now things are okay.
$ npm --version
6.14.16
$ npm install -g npm#8
npm ERR! EEXIST: file already exists, symlink ...
$ npm install -g --force npm#8
npm WARN using --force I sure hope you know what you are doing.
+ npm#8.19.2
$ npm --version
8.19.2
Note also that in my case, no 'sudo' was required, thanks to homebrew. To check if your npm is installed with homebrew, use brew list.
$ brew list | grep node
node#12

Puppeteer installation has error

I have problem with puppeteer. I cannot install it on my debian 8.7.1.
These are the steps:
1.install chrome:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt-get -f install
sudo dpkg -i google-chrome-stable_current_amd64.deb
2.install nodejs & puppeteer:
apt-get install curl
curl -sL https://deb.nodesource.com/setup_8.x | bash -
sudo apt-get install -y nodejs
npm i puppeteer
When I run npm i puppeteer, the errors are:
> puppeteer#0.11.0 install /root/node_modules/puppeteer
> node install.js
ERROR: Failed to download Chromium r499413! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.
Error: Download failed: server returned code 403. URL: https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/499413 /chrome-linux.zip
at ClientRequest.https.get.response (/root/node_modules/puppeteer/utils/ChromiumDownloader.js:195:21)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)
at ClientRequest.emit (events.js:210:7)
at HTTPParser.parserOnIncomingClient (_http_client.js:565:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:116:23)
at TLSSocket.socketOnData (_http_client.js:454:20)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at addChunk (_stream_readable.js:266:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! puppeteer#0.11.0 install: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the puppeteer#0.11.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2017-10-04T12_32_37_607Z-debug.log
I run the command npm config set PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true to skip download but it wants to download chromium yet.
Any idea to solve this problem?
Try below options;
1) npm config set puppeteer_skip_chromium_download true
2) Add puppeteer_skip_chromium_download=true to .npmrc file where the package.json location
After that run sudo npm install --unsafe-perm=true --allow-root
References;
https://github.com/GoogleChrome/puppeteer/issues/2270
https://medium.com/forcit/seo-for-single-page-applications-8543619e1d0c
upgrade your puppeteer version (optional)
npm set strict-ssl false
npm config set puppeteer_skip_chromium_download true
and npm install again.
try
sudo npm install puppeteer --unsafe-perm=true
Error: spawn EACCES
This roughly means that puppeteer cannot execute Chrome due to permissions issues. If you're having troubles I have a docker image here that you can use and interact with remotely. The issue with Chrome and many linux distros is that installation is quite a challenge to solve.
If you have chrome already you can use system variable to skip installation
https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#environment-variables
I faced a similar issue when I was using Puppeteer.
I see two parts to your problem,
1. The inability to download Chromium and getting 403 error other is
2. Error: spawn EACCES problem
I'll start with Error: spawn EACCES problem as it's more subtle and tricky
This is because your debian machine doesn't have the required dependencies installed in order to run Puppeteer. These dependencies differ between distributions(CentOS, Ubuntu/Debian etc...).
Follow these steps,
Install all the dependencies required. You can refer this link for the list, https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
After all the dependencies are installed, install npm and configure proxy in .npmrc if you are behind a VPC/proxy.
Then, as usual, go ahead with npm i puppeteer and proceed as always
Next to address your inability to download Chromium problem.
Are you behind a proxy? That was my issue.
I noticed on executing npm i puppeteer the script downloads Chromium from an external URL which from your log is https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/499413/chrome-linux.zip
npm is unable to download from this external URL because of the proxy blocking it.
The way I solved this issue was by configuring .npmrc file with the proxy settings
npm config set proxy http://<proxy_username>:<proxy_passowrd>#<proxy_host>:<proxy_port>
npm config set https-proxy http://<proxy_username>:<proxy_passowrd>#<proxy_host>:<proxy_port>
Note:
If your username/password contains any special characters you need to URL encode them. Eg. If your username is bruce\wayne then you need to URL encode it as bruce%5Cwayne where %5C="\"
If your proxy has no authentication then ignore and
Examples:
npm config set proxy http://bruce%5Cwayne:darkknight#11.434.2.23:8080
npm config set https-proxy http://bruce%5Cwayne:darkknight#11.434.2.23:8080
So, try configuring your .npmrc file using the commands given above and then try running npm i puppeteer. The proxy will no longer be blocking the external URL and you would be able to download Chromium.
Let me know if this works and reach out to me if you have any doubts.
I have faced similar issue. The best work around for this is to create a project using npm init, then install puppeteer using command "npm install puppeteer --save". This way chromium also gets installed with out any issues.Hope this helps..
Please set the following in .npmrc
puppeteer_download_host = https://npm.taobao.org/mirrors
or
npm config set puppeteer_download_host https://npm.taobao.org/mirrors

How to unlink the local package? Where npm stores links in windows env?

I linked a locally developed package using npm link command. Since then I am not able to unlink it back.
I am on windows 10.
I have tried:
npm unlink
npm remove
npm uninstall
Completely wipe out npm caches and global repo.
I would expect the package will be installed from the npm network repo now as i have:
"dependencies": {
"package": "^version"
}
in my package.json file but it is still installing the local linked version. Where npm stores links?
cd $(npm root -g)
rm -rf <package>

EACCES Error with Bower install?

I've read a few answers on StackOverflow & some other sites but none seem to fix the issue I'm having.
I'm installing AppGyver Add-ons, via terminal. Getting the following error:
BradMacBookPro:SparksInSpain breadadams$ bower install https://##MY-DOWNLOAD-CODE###addons.appgyver.com/steroids-addons.js?version=3.1.0 --save
bower not-cached https://##MY-DOWNLOAD-CODE###addons.appgyver.com/steroids-addons.js?version=3.1.0#*
bower resolve https://##MY-DOWNLOAD-CODE###addons.appgyver.com/steroids-addons.js?version=3.1.0#*
bower download https://##MY-DOWNLOAD-CODE###addons.appgyver.com/steroids-addons.js?version=3.1.0
bower EACCES EACCES, mkdir '/Users/breadadams/.cache/bower/packages/7480e059b318750565b4b25c2556c9c4'
Stack trace:
Error: EACCES, mkdir '/Users/breadadams/.cache/bower/packages/7480e059b318750565b4b25c2556c9c4'
Console trace:
Trace
at StandardRenderer.error (/usr/local/lib/node_modules/bower/lib/renderers/StandardRenderer.js:74:17)
at Logger.updateNotifier.packageName (/usr/local/lib/node_modules/bower/bin/bower:109:18)
at Logger.EventEmitter.emit (events.js:95:17)
at Logger.emit (/usr/local/lib/node_modules/bower/node_modules/bower-logger/lib/Logger.js:29:39)
at /usr/local/lib/node_modules/bower/lib/commands/install.js:27:16
at _rejected (/usr/local/lib/node_modules/bower/node_modules/q/q.js:808:24)
at /usr/local/lib/node_modules/bower/node_modules/q/q.js:834:30
at Promise.when (/usr/local/lib/node_modules/bower/node_modules/q/q.js:1079:31)
at Promise.promise.promiseDispatch (/usr/local/lib/node_modules/bower/node_modules/q/q.js:752:41)
at /usr/local/lib/node_modules/bower/node_modules/q/q.js:574:44
System info:
Bower version: 1.2.8
Node version: 0.10.25
OS: Darwin 13.1.0 x64
I'm not sudo, I've tried --allow-root, cleaned the bower cache, always get the same error on install. Some local permission problem?
I had a similar issue that I couldn't resolve using these suggestions. I was successful when I ran Bower with root privileges, like so:
sudo bower install --allow-root
Change the ownership for the .config and cache directory
sudo chown -R username:username ~/.config
and
sudo chown -R username:username ~/.cache
I noticed you tried this from a MacBook Pro.
You could try this:
go to the finder
under menu go Go To Folder type this: ~/.cache/bower
delete its content and try again.
Also as #Edwin suggested you may try:
The command bower cache clean which will have the same effect irrespective of OS.
Im not sure what happened with my project but in order to fix the issues with bower I had to run the following.
sudo npm install -g bower
sudo bower cache clean --allow-root
rm -Rf bower_components
sudo bower install --allow-root
Im still not really sure what went wrong but after those commands i was up and running
1) run
$groups
to get the name of your group
2) then run
$sudo chown -R {user}:{group} ~/.config
$sudo chown -R {user}:{group} ~/.cache
where {user} - is username and {group} - is group name
I hit the same thing and noticed that the bower_components folder was owned by the root user. I changed ownership to my user and everything worked without having to run bower as root.
sudo chown -R $USER bower_components
Like everyone says, bower and npm shouldn't be run with sudo.
This corrected the problem for me on my OSX machine.
sudo chown -R $USER:$GROUP ~/.npm
sudo chown -R $USER:$GROUP ~/.config
Now you should be able to run:
npm install
npm start
and even
bower install
without having to use sudo.
*note npm start usually calls bower install
I had the same issue but nothing above worked for me. If you recently updated Xcode and don't recognize, don't forget to agree the terms and install the command line tools!
xcode-select --install

Lesscss command line compiler Lessc

Hi Im trying to get the LessCss command line compiler installed on a Mac.
I've tried
brew install less
Error: No available formula for less
brew install lessc
Error: No available formula for lessc
sudo npm install -g less
..Installs happily..
sudo npm install -g lessc
npm http GET https://registry.npmjs.org/lessc
npm http 404 https://registry.npmjs.org/lessc
npm ERR! 404 'lessc' is not in the npm registry.
npm install less --global
..Installs happily..
npm install lessc --global
npm http GET https://registry.npmjs.org/lessc
npm http 404 https://registry.npmjs.org/lessc
npm ERR! 404 'lessc' is not in the npm registry.
and all I get is
less js/less/style.less > style-theme.css
-bash: less: command not found
lessc js/less/style.less > style-theme.css
-bash: lessc: command not found
Does anyone have any experience with this?
I found something said my path variable might be messed up. This:
/bin/echo $PATH
gets me:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin
the npm package name for lessc is less
try writing
sudo npm install -g less
I will try to provide a complete answer.
All commands must be executed in the Terminal application.
If you don't have it: Install Command Line Tools for Xcode
xcode-select --install
If you don't have it: Install HomeBrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
If you don't have it: Install Node.js
brew install node
Install LessCSS
sudo npm install -g less
Now you can execute lessc commands.
i`m using macOS Siera and work for me, you can try:
install npm using brew:
brew install node
check node and npm version using:
node -v
npm -v
3.change user to SU using:
sudo su
install less:
npm install -g less
Good Luck.....

Resources