Skip to content

Docs for switching node directory #802

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

Merged
merged 7 commits into from
May 9, 2024
Merged
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Node.js version management: no subshells, no profile setup, no convoluted API, j
- [Supported Platforms](#supported-platforms)
- [Installation](#installation)
- [Third Party Installers](#third-party-installers)
- [Replacing a previous node install](#replacing-a-previous-node-install)
- [Installing Node.js Versions](#installing-nodejs-versions)
- [Specifying Node.js Versions](#specifying-nodejs-versions)
- [Removing Versions](#removing-versions)
Expand Down Expand Up @@ -87,6 +88,18 @@ n-install sets both `PREFIX` and `N_PREFIX` to `$HOME/n`, installs `n` to `$HOME

As a result, both `n` itself and all Node.js versions it manages are hosted inside a single, optionally configurable directory, which you can later remove with the included `n-uninstall` script. `n-update` updates `n` itself to the latest version. See the [n-install repo](https://github.com/mklement0/n-install) for more details.

### Replacing a previous node install

Changing from a previous Node.js installed to a different location may involve a few extra steps. See docs for [changing node location]((./docs/changing-node-location.md)) for a walk-through example of switching from using Homebrew to using `n` to manage Node.js.

You have a problem with multiple versions if after installing node you see the "installed" and "active" locations are different:
```console
% n lts
copying : node/20.12.2
installed : v20.12.2 to /usr/local/bin/node
active : v21.7.3 at /opt/homebrew/bin/node
```

## Installing Node.js Versions

Simply execute `n <version>` to download and install a version of Node.js. If `<version>` has already been downloaded, `n` will install from its cache.
Expand Down
115 changes: 115 additions & 0 deletions docs/changing-node-location.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Switching To `n` Managed Node.js

If you already have Node.js installed to a different root than `n` uses, you can easily end up with multiple copies of node (and npm, and npx, and globally installed packages!). Some common situations are you already had Node.js installed using your Linux package manager, or using another node version manager, or using say Homebrew. The two main ways you might resolve this are:

- uninstall from the old directory and reinstall to the new directory
- put the `bin` directory that `n` uses early in the `PATH` environment variable, so the `n` installed node is found first

The simplest setup to understand is the first one. Just have one version of `node` installed.

Let's walk-through the process of switching over from using Homebrew as an example. Let's start off with Node.js installed, `npm` updated, and an example global npm package. The key point is there are two install prefixes involved:

- old: `/opt/homebrew`
- new: `/usr/local`

```console
% brew install node
% npm install --global npm@latest
% npm install --global @shadowspawn/forest-arborist
% brew list node
/opt/homebrew/Cellar/node/21.7.3/bin/node
/opt/homebrew/Cellar/node/21.7.3/bin/npm
/opt/homebrew/Cellar/node/21.7.3/bin/npx
/opt/homebrew/Cellar/node/21.7.3/etc/bash_completion.d/npm
/opt/homebrew/Cellar/node/21.7.3/include/node/ (107 files)
/opt/homebrew/Cellar/node/21.7.3/libexec/bin/ (2 files)
/opt/homebrew/Cellar/node/21.7.3/libexec/lib/ (2012 files)
/opt/homebrew/Cellar/node/21.7.3/share/doc/ (2 files)
/opt/homebrew/Cellar/node/21.7.3/share/man/man1/node.1
% command -v node
/opt/homebrew/bin/node
% command -v npm
/opt/homebrew/bin/npm
% npm prefix --global
/opt/homebrew
```

Before we start transferring, list the global npm packages in the "old" location. We will refer back to this list.

```console
% npm list --global
/opt/homebrew/lib
├── @shadowspawn/forest-arborist@12.0.0
└── npm@10.5.0
```

We could clean out the old location first, but let's install `n` and another copy of node and see what that looks like. We end up with two versions of node, and the active one is still the Homebrew managed version.

```console
% brew install n
% n lts
installing : node-v20.12.2
mkdir : /usr/local/n/versions/node/20.12.2
fetch : https://nodejs.org/dist/v20.12.2/node-v20.12.2-darwin-arm64.tar.xz
copying : node/20.12.2
installed : v20.12.2 to /usr/local/bin/node
active : v21.7.3 at /opt/homebrew/bin/node
% command -v node
/opt/homebrew/bin/node
% which -a node
/opt/homebrew/bin/node
/usr/local/bin/node
% command -v npm
/opt/homebrew/bin/npm
% command -v npx
/opt/homebrew/bin/npx
% n doctor
<...>

CHECKS

Checking n install destination is in PATH...
good

Checking n install destination priority in PATH...
⚠️ There is a version of node installed which will be found in PATH before the n installed version.

Checking npm install destination...
⚠️ There is an active version of npm shadowing the version installed by n. Check order of entries in PATH.
installed : /usr/local/bin/npm
active : /opt/homebrew/bin/npm

<...>
```

Now let's switch over. Delete everything from the old location. Delete all the global npm packages _except_ npm itself, then delete npm, then delete node.

```console
npm uninstall --global @shadowspawn/forest-arborist

npm uninstall --global npm

brew uninstall node
```

Check the active binaries are now the ones installed by `n`:
```console
% command -v node
/usr/local/bin/node
% command -v npm
/usr/local/bin/npm
% command -v npx
/usr/local/bin/npx
```

And lastly, reinstall the global npm packages you started with:
```
% npm prefix --global
/usr/local
% npm install --global npm@latest
% npm install --global @shadowspawn/forest-arborist
% npm list -g
/usr/local/lib
├── @shadowspawn/forest-arborist@12.0.0
└── npm@10.5.0
```