I am using nvm (Node Version Manager) to manage multiple versions of Node.js on my system and i would like to install a command-line tool globally so that it remains accessible even when I switch to a different Node.js version using the nvm use <version> command.

I want the tool to be available globally, similar to how pipx with Python.

I have tried installing the command-line tool globally using npm install -g <tool>. However, this approach only installs the tool within the Node.js folder of the specific version I am currently using with nvm. When I switch to a different Node.js version, the tool becomes inaccessible.

I Want the globally installed tool to remain accessible across all Node.js versions managed by nvm, regardless of the version I am currently using, so how can I achieve this?

1

There are 1 best solutions below

0
On

There's no way to do that with NVM because NVM keeps an isolated environment for each Node version.

You can make a script that installs a package on all versions that you have installed:

#!/bin/bash

PACKAGE="your-package"

versions=$(nvm ls --no-colors | grep -o -E 'v[0-9]+\.[0-9]+\.[0-9]+')

for version in $versions
do
  echo "Installing $PACKAGE on Node.js $version..."
  nvm install $version
  npm install -g $PACKAGE
done