fnm icon indicating copy to clipboard operation
fnm copied to clipboard

Support for reinstall packages from other versions

Open gperdomor opened this issue 2 years ago โ€ข 12 comments

Please add support for reinstall-packages-from flag like nvm ๐Ÿ™๐Ÿป

gperdomor avatar Jan 02 '22 20:01 gperdomor

Duplicate of #139 ?

Schniz avatar Jan 02 '22 21:01 Schniz

No, these are two distinct features.

ljharb avatar Jan 02 '22 21:01 ljharb

@ljharb looks like the issue I linked is all about โ€œreinstalling packages fromโ€. Is there a feature of default packages in nvm too?

Schniz avatar Jan 02 '22 21:01 Schniz

The one you linked's title and OP suggest it's about "installing packages by default with a newly installed node version", which is a feature of nvm.

This one is about nvm reinstall-packages X and nvm install X --reinstall-packages-from Y which are for "on-demand, install the global packages from one node version into the current/new one".

ljharb avatar Jan 02 '22 21:01 ljharb

thanks @ljharb, I read nvm's docs and now I understand. :)

Schniz avatar Jan 03 '22 09:01 Schniz

fnm install X --reinstall-packages-from Y would be handy for ensuring packages I install with npm i -g automatically follow me as I change versions. ๐Ÿ™

ooloth avatar Dec 16 '22 15:12 ooloth

I'd love to see this supported too, but in the mean time, this little shell command makes it a little easier to get a list of your installed global modules

npm list -g --json | jq -r '.dependencies | keys | join(" ")'

This assumed you have jq installed. It'll format all the installed modules in a single line that you can copy and paste (or redirect to your clipboard by adding | pbcopy on a Mac) into a new npm command to manually reinstall modules from a previous install.

Run this in the old version, of course. If you're adventurous, you could try something to install them automatically, but you'd need to redirect the output from an older node version to the new one. Somehow.

jschuur avatar Apr 18 '23 10:04 jschuur

@jschuur that will include npm as well, which could brick your target node version.

ljharb avatar Apr 18 '23 17:04 ljharb

@jschuur that will include npm as well, which could brick your target node version.

Are you suggesting if I run npm install -g npm (by copying it from a list of installed modules and accidentally including it) that it could brick my node version? How so?

Is this an fnm specific issue or a general possibility?

jschuur avatar Apr 19 '23 06:04 jschuur

I made a little shell script to help me with this. Just add it to your bashrc/zshrc. Hope this helps some people.

fnm-reinstall-packages-from () { npm install -g $(fnm exec --using=$1 npm list -g | grep "โ”œโ”€โ”€\|โ””โ”€โ”€" | awk '{gsub(/@[0-9.]+/, "", $2); print $2}' | tr '\n' ' ' | sed 's/ $//') }

Example Usage:

$ node --version
v18.12.0
$ fnm install v18.19.0
$ fnm use v18.19.0
$ fnm-reinstall-packages-from v18.12.0

aforty avatar Dec 08 '23 15:12 aforty

@jschuur yes, you can, because newer versions of npm don't necessarily work on older node versions, and once a broken one is installed, you're screwed.

ljharb avatar Dec 08 '23 17:12 ljharb

Here's a Nushell version:

npm list -g -json
| from json
| get dependencies
| items {|key, value| { name: $key, version: $value.version } }
| format "{name}@{version}"
| each { |r| npm install $r }

Southclaws avatar Apr 04 '24 11:04 Southclaws