Tool to change NPM version for testing - by symlin

2019-08-01 05:13发布

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?

标签: node.js npm
1条回答
smile是对你的礼貌
2楼-- · 2019-08-01 05:51

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" 
查看更多
登录 后发表回答