diff --git a/install.sh b/install.sh index 0e22f82..45070fc 100755 --- a/install.sh +++ b/install.sh @@ -120,6 +120,42 @@ nvm_detect_profile() { fi } +# +# Check whether the user has any globally-installed npm modules in their system +# Node, and warn them if so. +# +nvm_check_global_modules() { + local NPM_GLOBAL_MODULES + NPM_GLOBAL_MODULES=$(npm list -g --depth=0 | sed '/ npm@/d') + + local MODULE_COUNT + MODULE_COUNT=$( + printf %s\\n "$NPM_GLOBAL_MODULES" | + sed -ne '1!p' | # Remove the first line + wc -l | tr -d ' ' # Count entries + ) + + if [ $MODULE_COUNT -ne 0 ]; then + cat <<-'END_MESSAGE' + => You currently have modules installed globally with `npm`. These will no + => longer be linked to the active version of Node when you install a new node + => with `nvm`; and they may (depending on how you construct your `$PATH`) + => override the binaries of modules installed with `nvm`: + + END_MESSAGE + printf %s\\n "$NPM_GLOBAL_MODULES" + cat <<-'END_MESSAGE' + + => If you wish to uninstall them at a later point (or re-install them under your + => `nvm` Nodes), you can remove them from the system Node as follows: + + $ nvm use system + $ npm uninstall -g a_module + + END_MESSAGE + fi +} + nvm_do_install() { if [ -z "$METHOD" ]; then # Autodetect install method @@ -169,6 +205,8 @@ nvm_do_install() { fi fi + nvm_check_global_modules + echo "=> Close and reopen your terminal to start using nvm" nvm_reset } diff --git a/test/install_script/nvm_check_global_modules b/test/install_script/nvm_check_global_modules new file mode 100755 index 0000000..4a5661c --- /dev/null +++ b/test/install_script/nvm_check_global_modules @@ -0,0 +1,40 @@ +#!/bin/sh + +cleanup () { + rm -rf "$npm_config_prefix/lib" >/dev/null 2>&1 + unset npm_config_prefix + + unset -f setup cleanup die skip + unset message +} +die () { echo $@ ; cleanup ; exit 1; } + +NVM_ENV=testing . ../../install.sh + +setup () { + npm_config_prefix="$(pwd)" + export npm_config_prefix + mkdir -p "$npm_config_prefix/lib" +} + + +setup + +npm install -g nop >/dev/null 2>&1 +message=$(nvm_check_global_modules) +[ ! -z "$message" ] || die "nvm_check_global_modules should have printed a notice when npm had global modules installed" + +npm uninstall -g nop >/dev/null 2>&1 +message=$(nvm_check_global_modules) +[ -z "$message" ] || die "nvm_check_global_modules should not have printed a notice when npm had no global modules installed" + +# Faking an installation of npm +mkdir -p "$npm_config_prefix/lib/node_modules/npm" +cat <<'JSON' >"$npm_config_prefix/lib/node_modules/npm/package.json" +{ "name": "npm", "version": "0.0.1fake" } +JSON + +message=$(nvm_check_global_modules) +[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm had only itself installed as a global module" + +cleanup