2

NVM is a great tool to allow us to change Node versions at will. I am trying to develop something good that will allow users to change NPM versions at will, mostly for the purposes of testing library code, before release.

So I am working on '@oresoftware/npm.version', which has an executable called npmv which will change out the current npm version in the $PATH.

The user runs:

npmv use 5.3 

and if 5.3 is not installed, it will install it to a directory with the user home, then it will symlink that version to the global space.

Here is what I have:

#!/usr/bin/env bash set -e; desired_npm_version="$1" if [ -z "$desired_npm_version" ]; then echo >&2 "No desired npm version provided."; exit 1; fi desired_v="$npmvv/$desired_npm_version" if [ ! -d "$desired_v" ]; then mkdir -p "$desired_v"; cd "$desired_v"; npm init -f --silent; npm install "npm@$desired_npm_version" -f --silent fi cd "$npmvv/$desired_npm_version"; npm_root="$(npm root -g)"; npm_bin="$(npm bin -g)"; rm -rf "$npm_root/npm"; rm -rf "$npm_bin/npm"; rm -rf "$npm_bin/npx"; ln -sf node_modules/npm "$npm_root" ln -sf node_modules/.bin/npm "$npm_bin/npm" ln -sf node_modules/.bin/npx "$npm_bin/npx" # end 

don't run it, because it will probably break your NPM installation. I cannot figure out why it doesn't work, but it doesn't play well with NVM (Node Version Manager) at the very least.

Note that NPM only comes with two executables (npm, npx).

If you know what you are doing and see something that might be missing in the script, please let me know. The only thing I can think of, is that perhaps running ln -s <source-file> <target-link> against a that is itself a symlink, doesn't work?

1 Answer 1

0

I have something that works pretty damn well now, just using symlinks. One important thing to note is that npm <---> node compatibility is not guaranteed. Many npm versions are not compatible with older node versions or newer node versions.

https://github.com/ORESoftware/npm.version

the main idea, is that we install the newly desired NPM version in user home, and then use symlinks to symlink to that NPM version:

npm_bin="$(npm bin -g)" npm_root="$(npm root -g)" cd "$HOME/.npmv_stash/versions/6.1.0"; ln -s "$PWD/node_modules/npm" "$npm_root/npm"; npm_source="$(readlink -f "$PWD/node_modules/.bin/npm")"; npx_source="$(readlink -f "$PWD/node_modules/.bin/npx")"; ln -sf "$npm_source" "$npm_bin/npm" ln -sf "$npx_source" "$npm_bin/npx" 
Sign up to request clarification or add additional context in comments.

1 Comment

or just use the nvm tool's parameter, see latest update here stackoverflow.com/a/33575448/823282

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.