js新建一个node节点
If you work on multiple Node.js projects, you’ve probably run into this one time or another. You have the latest and greatest version of Node.js installed, and the project you’re about to work on requires an older version. In those situations, the Node Version Manager (nvm) has your back, allowing you to install multiple versions of Node.js and switch between them as you see fit.
如果您从事多个Node.js项目,则可能一次或两次都遇到这种情况。 您已安装了最新最好的Node.js版本,并且您要处理的项目需要较旧的版本。 在这种情况下, Node Version Manager (nvm)可以为您提供支持,从而允许您安装Node.js的多个版本并在您认为合适的情况下在它们之间切换。
入门 (Getting started)
To get started, you will need to install the Node Version Manager, or nvm
on your system. It is included by many package managers, or you can install it manually by running one of the following:
首先,您需要在系统上安装Node Version Manager或nvm
。 许多程序包管理器都包含此程序,或者您可以通过运行以下程序之一来手动安装它:
# If you have `curl` installed:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
# Or if you prefer `wget`:
$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
Once installed, you will also need to add a couple of lines to your shell’s startup file (.bashrc
, .bash_profile
, .zshrc
, et cetera):
安装后,您还需要在Shell的启动文件中添加几行( .bashrc
, .bash_profile
, .zshrc
等):
export NVM_DIR="${XDG_CONFIG_HOME/:-$HOME/.}nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Don’t forget to reload your configuration by source
ing the file, or simply closing and reopening your favorite terminal application.
不要忘记通过source
文件或简单地关闭并重新打开您喜欢的终端应用程序来重新加载配置。
You can also opt to paste those lines directly into your terminal each time you’d like to use nvm
.
每次使用nvm
您也可以选择将这些行直接粘贴到终端中。
安装多个Node.js版本 (Installing Multiple Node.js Versions)
Now that we have everything installed, let’s install a few different versions of Node.js:
既然已经安装了所有内容,那么我们来安装一些不同版本的Node.js:
$ nvm install 0.10
Downloading and installing node v0.10.48...
Downloading https://nodejs.org/dist/v0.10.48/node-v0.10.48-linux-x64.tar.xz...
########################################################################################
100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v0.10.48 (npm v2.15.1)
$ nvm install 8
Downloading and installing node v8.16.0...
Downloading https://nodejs.org/dist/v8.16.0/node-v8.16.0-linux-x64.tar.xz...
########################################################################################
100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v8.16.0 (npm v6.4.1)
$ nvm install 12
Downloading and installing node v12.7.0...
Downloading https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.xz...
########################################################################################
100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.7.0 (npm v6.10.0)
Upon running each command, nvm
will download the version of Node.js from the official website and install it. Once installed, it will also set the version you just installed as the active version.
运行每个命令后, nvm
将从官方网站下载并安装Node.js版本。 安装后,它还会将您刚安装的版本设置为活动版本。
If you were to run node --version
after each of the aforementioned commands, you’d see the most recent version of the respective major version.
如果要在上述每个命令之后运行node --version
,则会看到相应主版本的最新版本。
nvm
isn’t limited to major versions either, you could also run nvm install 12.0.0
to explicitly install the 12 dot oh dot oh version of Node.js.
nvm
也不限于主要版本,您也可以运行nvm install 12.0.0
来显式安装Node.js的12 nvm install 12.0.0
版本。
列出已安装的Node.js版本 (Listing Installed Node.js Versions)
With a handful of different versions of Node.js installed, we can run nvm
with the ls
argument to list out everything we have installed:
安装了少数不同版本的Node.js之后,我们可以使用ls
参数运行nvm
,以列出已安装的所有内容:
$ nvm ls
v0.10.48
v4.9.1
v6.10.3
v6.14.4
v8.4.0
v8.10.0
v10.13.0
v10.15.0
v10.15.3
-> v12.0.0
v12.7.0
system
default -> v10.15 (-> v10.15.3)
node -> stable (-> v12.7.0) (default)
stable -> 12.7 (-> v12.7.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> N/A)
lts/argon -> v4.9.1
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.0 (-> N/A)
lts/dubnium -> v10.16.0 (-> N/A)
Yes, as a matter of fact, I do have a bunch of different versions installed. Obviously, your output will probably look a little different than my own.
是的,事实上,我确实安装了许多不同的版本。 显然,您的输出可能看起来与我的输出有些不同。
The little ->
indicates the active version, and default ->
indicates the default version of Node.js, the one that will be available when you open a new shell.
小号->
表示活动版本, default ->
表示Node.js的默认版本,当您打开新外壳时,它将可用。
system
corresponds with the version of Node.js installed outside of nvm
on your system.
system
与system
上nvm
外部安装的Node.js版本相对应。
设置默认的Node.js版本 (Setting a Default Node.js Version)
Even with juggling multiple versions, there’s a good chance you have one version that you would prefer to run the majority of the time.
即使使用了多个版本,您也很有可能拥有一个大部分时间都希望运行的版本。
Often times, that would be the latest stable version of Node.js, leveraging nvm
for being able to tap older versions.
通常,这将是Node.js的最新稳定版本,利用nvm
能够利用较旧的版本。
To set the latest stable version as your default, run:
要将最新的稳定版本设置为默认版本,请运行:
$ nvm alias default stable
default -> stable (-> v12.7.0)
If you’re like me, you have a specific version number you would like to set as your default. To alias default to a specific version, run:
如果您像我一样,则可以将特定的版本号设置为默认版本号。 要将默认名称别名为特定版本,请运行:
$ nvm alias default 10.15
default -> 10.15 (-> v10.15.3)
Now every time you open a new shell, that version of Node.js will be immediately available.
现在,每次打开新的外壳程序时,该版本的Node.js将立即可用。
在Node.js版本之间切换 (Switching Between Node.js Versions)
What good are all these versions of Node.js if we can’t switch between them?
如果我们不能在它们之间切换,那么所有这些版本的Node.js有什么用?
To switch to a different version of Node.js, simply tell nvm
which version you’d like to use
:
要切换到不同版本的Node.js,只需告诉nvm
您要use
哪个版本:
$ nvm use 0.10
Now using node v0.10.48 (npm v2.15.1)
$ nvm use 8
Now using node v8.16.0 (npm v6.4.1)
$ nvm use 12
Now using node v12.7.0 (npm v6.10.0)
You can even switch back to your default version:
您甚至可以切换回默认版本:
$ nvm use default
Now using node v10.15.3 (npm v6.9.0)
删除Node.js版本 (Removing Node.js Versions)
As you saw earlier, I have a bunch of different versions of Node.js installed due to working on a variety of projects over the past year on this particular laptop.
如您先前所见,由于在过去一年中使用此特定笔记本电脑从事各种项目,因此我安装了许多不同版本的Node.js。
Between you and me, I don’t even remember which projects I had installed some of those versions for.
在您我之间,我什至都不记得我为哪些项目安装了其中一些版本。
Fortunately, we can remove Node.js versions just as easily as we installed them:
幸运的是,我们可以像安装它们一样轻松地删除Node.js版本:
$ nvm uninstall 0.10
Uninstalled node v0.10.48
$ nvm uninstall 4
Uninstalled node v4.9.1
Unfortunately, when you specify a major or minor version, nvm
will only uninstall
the latest installed version that matches the version number.
不幸的是,当您指定主要或次要版本时, nvm
将仅uninstall
与版本号匹配的最新安装版本。
Because I have 2 different version of Node.js version 6 installed, I have to run the uninstall
command for each version:
因为我安装了2个不同版本的Node.js版本6,所以我必须为每个版本运行uninstall
命令:
$ nvm uninstall 6
Uninstalled node v6.14.4
$ nvm uninstall 6
Uninstalled node v6.10.3
Worth noting, you can’t remove a version of Node.js that is currently in use and active.
值得注意的是,您无法删除当前正在使用且处于活动状态的Node.js版本。
卸载节点版本管理器 (Unloading Node Version Manager)
Had enough and want to get back to your system’s installed version of Node.js?
是否已经足够,并想回到系统的已安装Node.js版本?
You could simply switch the system
version the same way we switched to other versions by running:
您可以通过运行以下命令来切换system
版本,就像我们切换到其他版本一样:
$ nvm use system
Now using system version of node: v11.15.0 (npm v6.10.1)
Yes, my system is a bit behind right now.
是的,我的系统现在有点落后。
Or if you’d prefer to unload nvm
entirely for your session, you can simply run nvm unload
.
或者,如果您希望为会话完全卸载nvm
,则只需运行nvm unload
。
结论 (Conclusion)
Working on multiple projects that utilize different versions of Node.js doesn’t have to be a nightmare.
在使用不同版本的Node.js的多个项目上进行工作不必是一场噩梦。
Node Version Manager makes the process quite painless and even takes the headache out of remembering to switch versions by supporting automatic switching by way of an .nvmrc
file in your project’s root:
Node Version Manager通过在项目的根目录中通过.nvmrc
文件支持自动切换,使该过程非常轻松,甚至使您.nvmrc
为切换版本而.nvmrc
:
$ echo "12" > .nvmrc
And you’re on your way!
而您正在路上!
翻译自: https://www.digitalocean.com/community/tutorials/nodejs-node-version-manager
js新建一个node节点