Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --preserve-npm #513

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ Flags also work here:

### Working with `npm`

A node install normally includes npm as well, which might be a downgrade if you have upgraded npm separately. You can preserve your current npm and exclude it from the install:

$ n --preserve-npm 6.1.0

After switching Node.js versions using `n`, `npm` may not work properly. This should fix it (thanks [@mikemoser](https://github.com/mikemoser)!):

```sh
Expand Down Expand Up @@ -149,11 +153,12 @@ Output can also be obtained from `n --help`.

Options:

-V, --version Output current version of n
-h, --help Display help information
-q, --quiet Disable curl output (if available)
-d, --download Download only
-a, --arch Override system architecture
-V, --version Output current version of n
-h, --help Display help information
-q, --quiet Disable curl output (if available)
-d, --download Download only
-a, --arch Override system architecture
-p, --preserve-npm Preserve current npm during install or activate of node

Aliases:

Expand Down
20 changes: 13 additions & 7 deletions bin/n
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ DEFAULT=0
QUIET=true
ACTIVATE=true
ARCH=
PRESERVE_NPM=false

#
# set_default <BIN_NAME>
Expand Down Expand Up @@ -185,11 +186,12 @@ display_help() {

Options:

-V, --version Output current version of n
-h, --help Display help information
-q, --quiet Disable curl output (if available)
-d, --download Download only
-a, --arch Override system architecture
-V, --version Output current version of n
-h, --help Display help information
-q, --quiet Disable curl output (if available)
-d, --download Download only
-a, --arch Override system architecture
-p, --preserve-npm Preserve current npm during install or activate of node

Aliases:

Expand Down Expand Up @@ -487,14 +489,17 @@ activate() {
if test "$version" != "$active"; then
local dir=$BASE_VERSIONS_DIR/$version
# Remove old npm to avoid potential issues with simple overwrite.
if test -d "$dir/lib/node_modules/npm"; then
if [[ $PRESERVE_NPM == false && -d "$dir/lib/node_modules/npm" ]]; then
if test -d "$N_PREFIX/lib/node_modules/npm"; then
rm -rf "$N_PREFIX/lib/node_modules/npm"
fi
fi
# Copy (lib before bin to avoid error messages on Darwin when cp over dangling link)
for subdir in lib bin include share; do
if test -L "$N_PREFIX/$subdir"; then
if [[ $PRESERVE_NPM == true ]]; then
mkdir -p "$N_PREFIX/$subdir"
(cd "$dir/$subdir"; tar -cf - --exclude npm --exclude npx .) | (cd "$N_PREFIX/$subdir"; tar -xf -)
elif test -L "$N_PREFIX/$subdir"; then
find "$dir/$subdir" -mindepth 1 -maxdepth 1 -exec cp -fR "{}" "$N_PREFIX/$subdir" \;
else
cp -fR "$dir/$subdir" $N_PREFIX
Expand Down Expand Up @@ -778,6 +783,7 @@ else
-h|--help|help) display_help; exit ;;
-q|--quiet) set_quiet ;;
-d|--download) ACTIVATE=false ;;
-p|--preserve-npm) PRESERVE_NPM=true ;;
--latest) display_latest_version; exit ;;
--stable) display_latest_stable_version; exit ;;
--lts) display_latest_lts_version; exit ;;
Expand Down