sudo: npm: command not found, while running npm with sudo - node.js

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.

Related

I cannot run npm install

I've dual Boot(ubuntu/Windows 10) on my laptop.
I am getting this error and I can't run npm install on Ubuntu. I checked with node -v and it shows v17.4.0
:
npm does not support Node.js v13.14.0
npm ERR! Invalid or unexpected token
npm ERR! A complete log of this run can be found in:
npm ERR! /home/diana/.npm/_logs/2022-02-12T09_42_18_704Z-debug-0.log
Update: I had to uninstall and remove all the folders related to node and npm. I've also installed the latest version of nvm 0.39 and only then I managed to actually upgrade the node to the right version. Everything is working now.
How can I completely uninstall nodejs, npm and node in Ubuntu
Have you tried updating your npm?
If no, run this:
sudo apt-get install -y npm
or check this:
https://stackoverflow.com/questions/23393707/how-to-update-npm#:~:text=sudo%20add%2Dapt%2Drepository%20ppa%3Achris%2Dlea/node.js%0Asudo%20apt%2Dget%20update%0Asudo%20apt%2Dget%20install%20nodejs%20npm

npm install puppeteer showing permission denied errors

I'm unable to install puppeteer as a project dependency, and I've tried re-installing node. Anyone have an idea on how to fix this? Running Ubuntu 17.10 x64
sudo apt-get purge nodejs;
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -;
apt-get install -y nodejs;
sudo npm install -g n;
sudo n stable;
Node versions:
$ node -v
v9.4.0
$ npm -v
5.6.0
I try to install:
root#server:/var/www/html# npm install --save puppeteer
Error message:
> puppeteer#1.1.0 install /var/www/html/node_modules/puppeteer
> node install.js
ERROR: Failed to download Chromium r536395! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.
{ Error: EACCES: permission denied, mkdir '/var/www/html/node_modules/puppeteer/.local-chromium'
errno: -13,
code: 'EACCES',
syscall: 'mkdir',
path: '/var/www/html/node_modules/puppeteer/.local-chromium' }
npm WARN project#1.0.0 No description
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! puppeteer#1.1.0 install: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the puppeteer#1.1.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/2018-02-19T15_55_52_669Z-debug.log
I don't see any ways to fix this in the referenced issue: https://github.com/GoogleChrome/puppeteer/issues/375
EDIT 20th April 2019:
The easy solution suggested by lauraalvarezz1 is,
sudo npm install -g puppeteer --unsafe-perm=true
This is okay as long as you trust puppeteer and want it to install puppeteer globally.
However beware of using --unsafe-perm=true for permission related problems. Reasons are:
Running unsafe-perm=true with sudo, as a non-root user, will give the script root access. This might be okay only if you trust the script and do not concern about security that much.
You might need to use --no-sandbox in every script you run, because the chrome installed with this command might not run without this parameter. See this github issue.
You have installed npm with sudo. Thus anything you install globally will require sudo.
To install anything on var/www/html folder, either you have to own that folder,
sudo chown -R $USER /var/www/html
Or you can use nvm to manage npm. Technically it will use your home directory and your current user.
After installing nvm, you can install puppeteer globally with it,
npm i -g puppeteer
or you have to use sudo
sudo npm install --save puppeteer
However chromium will not be downloaded due to permission error, that's why you have to use ---unsafe-perm=true as stated before.
Security Related Resources:
Resolve this without sudo, you can use this answer.
Learn more about best practices dealing with /var/www folder, refer to this answer.
Best of luck!
Run this on your terminal:
sudo npm install -g puppeteer --unsafe-perm=true
Before you begin, make sure you have the most recent version of Node.js.
The Puppeteer Documentation states:
Note: Puppeteer requires at least Node v6.4.0, but the examples below use async/await which is only supported in Node v7.6.0 or greater.
You can check which version of Node.js you have using the following command:
node -v
# OR nodejs -v
If your version of Node.js is less then v7.6.0, you can completely uninstall your current version of Node.js.
Then, you can use complete the reinstallation using a PPA:
sudo apt update
curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install nodejs
sudo apt install build-essential
You can check the new versions of Node.js and NPM installed on your system:
node -v
npm -v
Finally, you can install Puppeteer:
sudo npm install puppeteer --unsafe-perm=true --allow-root
Now you can run Puppeteer scripts using the node command:
node puppeteer-script.js
I did:
yarn install --ignore-scripts
yarn install.
And it worked without sudo or global package installation.
It should work with npm too.
I have Ubuntu 20.04.

Unable to run npm on mac terminal

I have previously installed node and I can see I have version v4.2.2 on my mac when running node -v. I'm now trying to update to latest version of node as well as install grommet.js
When enter on terminal:
npm install npm#latest -g
or
npm install -g grommet-cli
I get
-bash: npm: command not found
I tried resolving with steps in link below without success:
Upgrade NodeJS to the latest version on Mac os
How should I go about resolving this one?
It's likely that npm might be broken on your system. Might want to try and reinstall npm.
curl -L https://www.npmjs.org/install.sh | sh
See https://github.com/npm/npm/wiki/Troubleshooting for additional info.

sudo npm command not found (node installed from NVM)

I just install node.js 4.4.4 from NVM on ubuntu 14.04.
npm --version give me 2.15.1
sudo npm --version give me sudo: npm: command not found
I first tried to solved my problem with that Q&A but as I installed node with NVM then node is install into ~/.nvm/ instead of the default location ...
Note whereis npm give me npm: with nothing after
EDIT
I've just read that NVM should remove sudo requirement in node process. But when I try npm install pm2 -g I receive npm ERR! Please try running this command again as root/Administrator. which doesn't make sens ...

ubuntu npm related questions

I installed npm and node via command
brew install node successfully. And the terminal showed the version of npm and node when I typed in node -v and npm -v.
However, when I tried to install Karma via sudo npm install -g karma, the terminal showed npm:command not found. And I turned to /usr/bin, there was no npm there.
What's wrong with my installation? Thanks!

Resources