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
nvmwith this install instruction. It's not only a node version manager, but also lets you usenpmcommand withoutsudo.After running the
curlorwgetinstallation script, you will need to close the terminal window as the current terminal window doesn't know thenvmcommand yet. -
Run
nvm install nodeto 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:


---
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 -gfor whatever reason, then no need to usenvmeither: configure aprefixsomewhere in your home directory for NPM to install to and add itsnode_modulesto yourNODE_PATH(and make sure to put itsbin/in your path).And if you're using
npm install -gto 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 bothnodeandnpm, 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
npxyou have even fewer steps: none. 😉To update
npm, all you need to do isnpm update -glike you would do withoutnvm.nvmis simply a node version manager that lets you develop on a specificnodeversion 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
nodeversion separately.I believe
npmandnpxhave difference use cases.Some comments may only be visible to logged-in visitors. Sign in to view all comments.