Skip to content

Instantly share code, notes, and snippets.

@olets
Last active May 19, 2021 15:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olets/2fb5eecb2efc39a73d2a37a1c2975e13 to your computer and use it in GitHub Desktop.
Save olets/2fb5eecb2efc39a73d2a37a1c2975e13 to your computer and use it in GitHub Desktop.
List global packages for all nvm Nodes
#!/usr/bin/env sh
# nvm-ls-g
# Henry Bley-Vroman
# MIT License, 2018
# Lists the globally-installed npm packages
# for every nvm-installed version of Node
# Usage:
# `nvms-ls-g`
# or e.g. `nvm-ls-g | less`
# Error exit statuses:
# 1: $NVM_DIR not defined
list_items() {
heading="$1"
items="$2"
printf "%s\\n======\\n%s\\n\\n" "$heading" "$items"
}
if ! [ -d "$NVM_DIR" ]; then
printf "\$NVM_DIR not defined. Make sure nvm is correctly installed.\\n"
exit 1
fi
printf "Note: nvm automatically installs npm.\\n\\n"
for item in $NVM_DIR/versions/node/*; do
if [ -d "$item" ]; then
node_version=$(basename "$item")
packages=$(find "$item"/lib/node_modules -type d -mindepth 1 -maxdepth 1 -exec basename {} \;)
list_items "$node_version" "$packages"
fi
done
if [ -f "$NVM_DIR"/default-packages ]; then
default_packages=$(cat "$NVM_DIR"/default-packages)
list_items "Default packages" "$default_packages"
fi
@blagusps
Copy link

blagusps commented May 19, 2021

I am getting a warning:
find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
To fix, change line 33 to:
packages=$(find "$item"/lib/node_modules -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment