=> 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`:/root/.local/share/fnm/node-versions/v22.11.0/installation/lib├── @quasar/cli@2.4.1├── corepack@0.29.4├── netlify-cli@17.37.2├── node-gyp@11.0.0├── node-sass@9.0.0├── node-waf@10.1.1├── pm2@5.4.3├── pnpm@9.13.1├── quasar@2.17.4├── sass@1.82.0├── serve@14.2.4├── vercel@39.0.2├── vue-cli@2.9.6└── yarn@1.22.22=> 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=> Close and reopen your terminal to start using nvmroot@racknerd-626f077:~# )
或者使用 Homebrew 来安装。安装完后,还需要修改一下 shell 配置(~/.zshrc or whatever),具体参见官方文档。
然后我们可以使用 nvm 来安装不同版本的 node。
在安装的时候,nvm 将不同的 node 版本存储到 ~/.nvm/<version>/ 下,然后修改 $PATH,将指定版本的 node 路径加入,这样我们调用的 node 命令即是使用指定版本的 node。
nvm 显然比 n 要复杂一些,但是另一方面,由于它是一个独立软件包,因此它和 node 之间的关系看上去更合乎逻辑:nvm 不依赖 node 环境,是 node 依赖 nvm;而不像 n 那样产生类似循环依赖的问题。
from http://taobaofed.org/blog/2015/11/17/nvm-or-n/
(我更愿意使用nvm)
----------------------------
Node.js – `NVM` vs `N`
I recently started doing an online course on “MEAN” applications for fun. If you’re not familiar, MEAN stands for Mongo, Express, Angular, Node.
It’s pretty neat to see the kinds of things that you can throw
together. I still don’t think I’d write something for production that
uses Node.js, but other people have had good experience with it.
While doing my learning, the course recommended not to just install
node, but to install it with nvm, n or something similar. I could guess
by the name that they’re similar to rvm and things similar to rvm (which
I finally got around to learning about), but that was about it.
I wanted to know the differences between them, and was hoping someone
else had already done the work, but unfortunately googling “nvm vs n”
didn’t yield the best results. The most useful article I could find was this.
Unfortunately, it was only partially informative – it taught me more
than I knew before, but I was interested in learning some of the finer
details. So I thought it warranted playing with them both.
Both N and NVM are just node.js version managers. That’s it. They
won’t magically write your projects for you, but they can help you keep
things separated when working on multiple projects, which as we all know
happens too often.
N
Github link
N is pretty easy to get installed. First you install node.js, then you run
npm install-gn. The irony is not lost on me that you must first install node in order to install the node manager.
The way N works is it installs the
n executable to your path. You then use it to install different versions of node.
1
2
3
ninstall0.10.33
ninstall0.11.7
ninstall0.8.25
and you can switch between them just by running
n and picking the version you want from a list.
Here’s where things get tricky.
N works by storing different versions of node in some directory somewhere and when you switch versions, it will copy it to
/usr/local/bin. This is great for simplicity. It’s easy to install, easy to use, the only problem is that you basically have to
sudo every command.
Also, because it’s just swapping out different versions of node from
your install directory, it doesn’t have a good way of handling those
pesky
npm install-g packages.
If you install something globally, then switch out versions of node, you may be left confused as to why things aren’t working.
NVM
Github link
NVM works a little differently. To install it, run
It might not install itself quite right in os x, so you may need to check to make sure it put this in your
~/.bash_profile
1
2
export NVM_DIR="/Users/<your user here>/.nvm"
[-s"$NVM_DIR/nvm.sh"]&&."$NVM_DIR/nvm.sh"# This loads nvm
on my machine, it put that in
.bashrc even though that file doesn’t work in os x.
Then you need to restart your terminal to get your path working, and you can start installing node versions like this
1
2
3
nvm install0.10.33
nvm install0.11.7
nvm install0.8.25
When NVM installs versions of node, it works a lot more like RVM. NVM installs node to
~/.nvm/<version>/. Then it modifies your PATH to include the proper bin directory.
This does make things slightly more complicated than it does with N,
but it does grant one benefit. When installing global packages, they too
get installed under
~/.nvm/<version>.
So if you’re working on two different projects and each of them needs a
version of karma-cli, you can be sure you’re using the proper version.
Now node is still fairly new, so there aren’t that many versions of the
different packages that tend to be installed globally, but give it time,
you never know what will happen.
The only thing that NVM is missing in my mind is an equivalent to RVM’s gemsets.
from https://encyclopediaofdaniel.com/blog/node-js-nvm-vs-n/
-------------
Updating Homebrew...
^C
==> Downloading https://github.com/creationix/nvm/archive/v0.34.0.tar.gz
==> Downloading from https://codeload.github.com/nvm-sh/nvm/tar.gz/v0.34.0
######################################################################## 100.0%
==> Caveats
Please note that upstream has asked us to make explicit managing
nvm via Homebrew is unsupported by them and you should check any
problems against the standard nvm install method prior to reporting.
You should create NVM's working directory if it doesn't exist:
mkdir ~/.nvm
Add the following to ~/.bash_profile or your desired shell
configuration file:
You can set $NVM_DIR to any location, but leaving it unchanged from
/usr/local/opt/nvm will destroy any nvm-installed Node installations
upon upgrade/reinstall.
Type `nvm help` for further information.
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
==> Summary
🍺 /usr/local/Cellar/nvm/0.34.0: 7 files, 141.7KB, built in 28 seconds
---------------------------------------------------------------------------------------------
(Unfortunately n is not supported on Windows yet. If you're able to make it work, send in a pull request!)
Installation
Since you probably already have node, the easiest way to install n is through npm:
$ npm install -g n
Alternatively, you can clone this repo and
$ make install
to install n to bin/n of the directory specified in the environment variable $PREFIX, which defaults to /usr/local (note that you will likely need to use sudo). To install n in a custom location (such as $CUSTOM_LOCATION/bin/n), run PREFIX=$CUSTOM_LOCATION make install.
Once installed, n installs node versions to subdirectory n/versions of the directory specified in environment variable N_PREFIX, which defaults to /usr/local; the activenode/iojs version is installed directly in N_PREFIX. To change the default to, say, $HOME, prefix later calls to n with N_PREFIX=$HOME or add export N_PREFIX=$HOME to your shell initialization file.
Additionally, consider third-party installer n-install, which allows installation directly from GitHub; for instance,
curl -L https://git.io/n-install | bash
sets both PREFIX and N_PREFIX to $HOME/n, installs n to $HOME/n/bin, modifies the initialization files of supported shells to export N_PREFIX and add $HOME/n/bin to the PATH, and installs the latest stable node version.
As a result, both n itself and all node 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 for more details.
Installing/Activating Versions
Simply execute n <version> to install a version of node. If <version> has already been installed (via n), n will activate that version.
$ n 0.8.14
$ n 0.8.17
$ n 0.9.6
Execute n on its own to view your currently installed versions. Use the up and down arrow keys to navigate and press enter to select. Use q or ^C (control + C) to exit the selection screen. If you like vim key bindings during the selection of node versions, you can use j and k to navigate up or down without using arrows.
$ n
0.8.14
ο 0.8.17
0.9.6
Use or install the latest official release:
$ n latest
Use or install the latest LTS official release:
$ n lts
Removing Versions
Remove some versions:
$ n rm 0.9.4 v0.10.0
Alternatively, you can use - in lieu of rm:
$ n - 0.9.4
Removing all versions except the current version:
$ n prune
Binary Usage
When running multiple versions of node, we can target them directly by asking n for the binary path:
$ n bin 0.9.4
/usr/local/n/versions/0.9.4/bin/node
Or by using a specific version through n's use sub-command:
$ n use 0.9.4 some.js
Flags also work here:
$ n as 0.9.4 --debug some.js
Working with npm
After switching Node.js versions using n, npm may not work properly. This should fix it (thanks @mikemoser!):
$ curl -0 -L https://npmjs.com/install.sh | sudo sh
sudo may not be required depending on your system configuration.
Usage
Output can also be obtained from n --help.
Usage: n [options/env] [COMMAND] [args]
Environments:
n [COMMAND] [args] Uses default env (node)
n io [COMMAND] Sets env as io
Commands:
n Output versions installed
n latest Install or activate the latest node release
n -a x86 latest As above but force 32 bit architecture
n lts Install or activate the latest LTS node release
n <version> Install node <version>n use <version> [args ...] Execute node <version> with [args ...]
n bin <version> Output bin path for <version>
n rm <version ...> Remove the given version(s)
n prune Remove all versions except the current version
n --latest Output the latest node version available
n --lts Output the latest LTS node version available
n ls Output the versions of node available
(iojs):
n io latest Install or activate the latest iojs release
n io -a x86 latest As above but force 32 bit architecture
n io <version> Install iojs <version>
n io use <version> [args ...] Execute iojs <version> with [args ...]
n io bin <version> Output bin path for <version>
n io rm <version ...> Remove the given version(s)
n io --latest Output the latest iojs version available
n io ls Output the versions of iojs available
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
Aliases:
which bin
use as
list ls
- rm
stable lts
Custom source
If you would like to use a project other than the official Node.js or io.js projects, you can use the special n project [command] which allows you to control the behavior of n using environment variables.
For example, to grab the latest io.js version but name it "foo" instead,
PROJECT_NAME="foo" PROJECT_URL="https://iojs.org/dist/" n project latest
Required Variables:
PROJECT_NAME: The name the project will be stored under
PROJECT_URL: The location to download the project from. Note, this must follow the same format as the io.js/Node.js repos
Optional Variables:
HTTP_USER: The username if the PROJECT_URL is protected by basic authentication
HTTP_PASSWORD: The password if the PROJECT_URL is protected by basic authentication
PROJECT_VERSION_CHECK: Many custom projects keep the same version number as the Node.js release they are based on, and maintain their own separate version in process. This allows you to define a JavaScript variable that will be used to check for the version of the process, for example: process.versions.node
Custom architecture
By default n picks the binaries matching your system architecture, e.g. n will download 64 bit binaries for a 64 bit system. You can override this by using the -a or --arch option.
Download and use latest 32 bit version of node:
$ n --arch x86 latest
Download and use latest 32 bit version of iojs:
$ n io --arch x86 latest
Download and use 64 bit LTS version of node for older Mac Intel Core 2 Duo systems (x86 image is no longer available but x64 runs fine):
$ n --arch x64 lts
Additional Details
n installs versions to /usr/local/n/versions by default. Here, it can see what versions are currently installed and activate previously installed versions accordingly when n <version> is invoked again.
Activated versions are then installed to the prefix /usr/local, which may be altered via the N_PREFIX environment variable.
To alter where n operates, simply export N_PREFIX.
n does not work on Windows at the moment. Pull Requests are appreciated.
If you are searching for the latest version of n below 2.x.x, check out the branch 1.x.x.