If you're a Node.js developer, you might be familiar with the npm install -g packagename
command. However, if you run the command without sudo
, you will likely face with the permission issue. This is due to your npm
installation method.
In fact, you shouldn't use sudo
with npm
at all. See more about this here.
Installing npm
the right way
First, if you installed npm
via your repo's package manager, please uninstall it first!, either by using YaST Software Management and search for npm
, or using this command:
sudo zypper remove npm
Then, follow these steps:
-
Install
nvm
with this install instruction. It's not only a node version manager, but also lets you usenpm
command withoutsudo
.After running the
curl
orwget
installation script, you will need to close the terminal window as the current terminal window doesn't know thenvm
command yet. -
Run
nvm install node
to install the latestnode
(Node.js) along with the latestnpm
. It should return something like this:
Downloading and installing node v20.0.0...
Downloading https://nodejs.org/dist/v20.0.0/node-v20.0.0-linux-x64.tar.xz...
############################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.0.0 (npm v9.6.4)
Creating default alias: default -> node (-> v20.0.0)
> Note, the `node` installing via `nvm` will use its version by default, not the version on your system.
3. If you want to update `nvm`, simply running the latest installation script on **[this page](https://github.com/nvm-sh/nvm#installing-and-updating)**. You can monitor a new release of `nvm` version by watching their git as shown in the screenshot below:
![monitor a new version #1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mitjzytcteln3nnv5vb7.png)
![monitor a new version #2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6tkkzbpqy9psxc572ut.png)
---
I hope this helps, bye 💨
---
**Cover** Photo by **[GuerrillaBuzz](https://unsplash.com/@guerrillabuzz?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)** on **[Unsplash](https://unsplash.com/photos/1_5FWSsyDmc?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)**
Top comments (7)
You didn't really explain how that's better, and the implications.
My opinion: want something better than
npm install -g
? Just usenpx
.That said, if you do want to use
npm install -g
for whatever reason, then no need to usenvm
either: configure aprefix
somewhere in your home directory for NPM to install to and add itsnode_modules
to yourNODE_PATH
(and make sure to put itsbin/
in your path).And if you're using
npm install -g
to install Yarn or PNPM, consider using Corepack instead: nodejs.org/api/corepack.htmlTo me, using NVM really seems like cargocult.
Thanks for your suggestion ❤️
However, the reason I think this method is the best is due to its simplicity. I don't have to configure anything in my system. A command for installing
nvm
, and another command for installing bothnode
andnpm
, are all I need to have a working environment.…but then you have (correct me if I'm wrong) to manually update Node and NPM (which means you have to follow the news about it), rather than simply relying on your system updates (assuming Linux here)
And with
npx
you have even fewer steps: none. 😉To update
npm
, all you need to do isnpm update -g
like you would do withoutnvm
.nvm
is simply a node version manager that lets you develop on a specificnode
version easily (and without needing the root permission).To update
node
, it'snvm install node
.And for development purposes (debugging and so on), it's even better to update the system and
node
version separately.I believe
npm
andnpx
have difference use cases.Some comments may only be visible to logged-in visitors. Sign in to view all comments.