tj / n

Node version management

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Maintaining global packages after installing new nodejs version

hongz1 opened this issue · comments

I am new to use this package and looking for the best efficient way to maintain existing global packages installed.

[Original state before installing n]
my current node version is 18.17.1 and pm2 global package is installed in /usr/lib/node_modules already in Ubuntu (22.04).

[After installing n package with node version 20]
pm2 package is not showing in the global package list, but I can still run pm2.

Q1: Does it have global package migrations feature?

Q2: (if not) Should I uninstall existing global packages and reinstall them after installing new node version via 'n'?

I am not sure the best approach to manage existing global packages....

Thanks.

A1: n does not have a global package migrations feature. n installs to a single prefix so you don't need to reinstall global packages when switching node versions using n.

A2: I think uninstalling the global packages from the old location is sensible so you don't have the ongoing confusion of packages installed in two locations.

You can do this without needing to reinstall node and npm by specifying the prefix explicitly, which you worked out is /usr for your old install. Doing one package at a time and not trying a fancy all-at-once...

# check what is installed in old location so can reinstall as needed
$ npm --prefix=/usr list -g  

# delete pm2 from old location
$ npm --prefix=/usr uninstall -g pm2

# check prefix is pointing at expected /usr/local before reinstalling
$ npm prefix -g
/usr/local

# check what is currently installed
$ npm list -g

# install pm2 if needed
$ npm install -g pm2

Thank you.