sudo npm fails, env: node: No such file or directory - node.js

After uninstalling and reinstalling node and npm I can no longer run npm as sudo. This is on a Mac. I have tried the official node installer and brew.
It works from my user but any attempt to run sudo npm produces this error:
$ sudo npm
env: node: No such file or directory

npm most likely is installed at /usr/local, but /usr/local is not on the $PATH of the root user. You can configure sudo to allow the $PATH to be changed, but if you installed npm with Homebrew you shouldn't have to use sudo at all.

Related

npm global install of ganache-cli fails (because of sudo/permissions)

I have npm version 6.5.0 and node v10.10.0.
There have been no previous issues with npm global installs.
sudo npm install --global ganache-cli
npm ERR! code 128
npm ERR! Command failed: /usr/bin/git clone --depth=1 -q -b master https://github.com/ethereumjs/ethereumjs-abi.git /root/.npm/_cacache/tmp/git-clone-305900d8
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-305900d8': Permission denied
npm ERR!
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-12-12T20_49_21_325Z-debug.log
The basic problem is that globally installing packages with npm links them into directories under /usr/local/, which are typically owned by the root user.
Node Version Manager (nvm) is one way to circumvent such tricky permissions issues. It allows globally installed packages to be linked into directories in your home folder under ~/.nvm/versions/node/<version>
Using my package manager, I removed node and related software such as npm.
I used find /usr/local/ -name 'node_modules' to scour /usr/local/ for any vestiges of the packages I had previously installed globally with npm. Delete them all.
Then I installed nvm using the latest install script from https://github.com/creationix/nvm
In my case it was
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
I had to then link nvm into my environment path.
In my case, this meant manually editing my .bashrc profile to include this:
export NVM_DIR="/home/zack/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
From there, I was ready to get the most recent LTS version of node through nvm
nvm install --lts
Now globally installed packages can be installed without sudo because they are deposited under ~/.nvm/versions/node/v10.14.2/ and everything works perfectly!

sudo: npm: command not found, while running npm with sudo

I installed nodejs v8.11.2 and npm v5.6.0 using the command nvm install 8.11.2.
When I opened my system after shutting it down for a while my system and wrote npm -v in the terminal, it said npm is not installed, and to install it with sudo apt install npm.
However when I again entered nvm install 8.11.2, it said that nodejs and npm are already installed and it began using it. Sometimes when opening a different terminal, npm -v says that npm is not installed. And later when I used the command sudo npm install, the output came:
sudo: npm: command not found
So I installed npm with sudo apt-get install npm, but it installed npm v3.5.2 and updating it with npm install npm#latest -g did nothing, i.e., it remained npm v3.5.2.
I need npm v5.6.0 for a project of mine and is clueless on how to get this issue sorted. Please help.
(nvm maintainer here)
sudo is not, and must not be, used with nvm. nvm is per-user, per-shell.
npm should not be installed by itself; it comes with node. You should apt-get remove it.
If nvm isn't being loaded properly on a new shell, try rerunning the install script, then file an issue on http://nvm.sh, and I'll be happy to help you directly.

npm install: Error: EACCES: permission denied, mkdir

I attempted to do a sudo npm install -g appium on Mac OS 10.12.5.
I get this error:
info Chromedriver Install Installing Chromedriver version '2.30' for platform 'mac' and architecture '64'
info Chromedriver Install Opening temp file to write chromedriver_mac64 to...
Error: EACCES: permission denied, mkdir
'/usr/local/lib/node_modules/appium/node_modules/appium-chromedriver/2017820-44752-12jfqpb.z2hd'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! appium-chromedriver#3.0.1 install: node install-npm.js
npm ERR! Exit status 1
npm ERR!
this is not a dup question, as this install attempt was with sudo, as the other one was not.
sudo npm install -g appium --unsafe-perm=true --allow-root
Worked for me
you are using npm so you have to use
sudo npm install --unsafe-perm
In unsafe mode with every command you run
hopefully, it will help
The -g option means install globally. When packages are installed globally, EACCES permission errors can occur.
Consider setting up npm to operate globally without elevated permissions. See Resolving Permission Errors for more information.
Option 1
The best way to avoid permission issues is to reinstall NodeJS and npm using a node version manager.
1. Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
You can close and reopen the terminal ou just open another terminal and check if nvm is installed properly with this command: command -v nvm.
2. To download and install the latest LTS release of NodeJS, run:
nvm install --lts
3. Set the newly installed NodeJS as the default environment:
nvm alias default lts/*
Option 2 (Does not apply for windows)
Change the owner of npm's directories to the current user:
sudo chown -R $(your_user) /usr/local/{lib/node_modules,bin,share}
sudo chown -R $(your_user) ~/.npm ~/.npmrc
I reinstalled Node/NPM. Problem solved.

sudo npm install command not found

I have installed nodejs and npm in my server (os:amazon ami) using node version manager
current node version is 7.10.0
npm version is 4.2.0
nodejs project is located in var/www/testing folder when i am trying to install the depeondeies present in package.json using sudo npm install i got command not found error
Path of node and npm is respectively
~/.nvm/versions/node/v7.10.0/bin/node
~/.nvm/versions/node/v7.10.0/bin/npm
npm and node are in same folder but when I try this command:
sudo npm install I got sudo: npm: command not found
npm install command throws no errors but node_modules folder is empty.
Correct npm path is /usr/local/bin/npm else just run below command again
sudo apt-get install npm
And verify the npm is install, Alternatively you can try:
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
I got this error when I tried to install npm.
bash-3.2$ sudo npm install
sudo: npm: command not found
So, I downloaded node.js from https://nodejs.org/en/
It will automatically set at the required path.
After installation, it worked for me.
Try To Install Node Using Nvm Node Version Manager
From Officai NVM Git Hub Repo
https://github.com/nvm-sh/nvm
git clone https://github.com/nvm-sh/nvm
cd nvm
./installer.sh
nvm install <AnyNodeVersion>
Now Hit Command Line
This Method Actually Worked For ME
i Face same Issue And Explained It Here
https://stackoverflow.com/questions/67285266/npmcommand-not-found-node-v-is-working-but-for-npm-command-not-found

npm init install project within /home/myUser folder on ubuntu 14

I'm using Ubuntu 14.04 and I've installed nodejs v5.1.0 via nvm, and npm v3.3.12. I also installed yeoman with some generators. However, if I try to init a new project (via npm init or yo some-generator), all the files and "node_modules" folder are installed into /home/myUser, without errors. Even if I start a new project into a different directory. I don't know how do configure this.
Can anyone help with this issue please??
I think your issues are permission npm:
Install npm with global: sudo npm install -g npm
Set full permission for npm: sudo chown -R $USER ~/.npm

Resources