written by cursor ide and claude 3.7 model
#!/bin/bash
# nvm-global-packages-inventory.sh
#
# Purpose: Generate an inventory of all globally installed NPM packages across
# different Node.js versions managed by NVM. This helps when migrating from
# NVM to ASDF or other version managers by documenting what packages were installed.
#
# Output format: PackageName PackageVersion NodeVersion
# Ensure NVM is loaded
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
echo "=== Global NPM Packages Inventory Across Node Versions ==="
echo "PackageName | PackageVersion | NodeVersion"
echo "-----------|----------------|------------"
# Get all installed Node versions
VERSIONS=$(nvm ls --no-colors | grep -o "v[0-9]\+\.[0-9]\+\.[0-9]\+" | sort -u)
for VERSION in $VERSIONS; do
echo "Processing Node $VERSION..."
# Switch to this Node version
nvm use "$VERSION" > /dev/null 2>&1
# Get current Node version (to verify)
CURRENT_NODE=$(node -v)
# List global packages excluding npm itself
npm list -g --depth=0 | grep -v "npm" | grep -o "@\?[a-zA-Z0-9_-]\+@[0-9]\+\.[0-9]\+\.[0-9]\+" | while read PACKAGE; do
# Extract package name and version
PKG_NAME=$(echo "$PACKAGE" | sed -E 's/(.+)@[0-9]+\.[0-9]+\.[0-9]+.*/\1/')
PKG_VERSION=$(echo "$PACKAGE" | sed -E 's/.+@([0-9]+\.[0-9]+\.[0-9]+.*)/\1/')
# Output in format: PackageName PackageVersion NodeVersion
echo "$PKG_NAME | $PKG_VERSION | $CURRENT_NODE"
done
done
echo ""
echo "Inventory complete! Use this information when setting up ASDF."