字體:  

如何在 CentOS 7 使用 nvm 安裝 nodejs 9.4 與 npm 5.6

cuteftp 發表於: 2018-1-29 08:46 來源: ADJ網路控股集團


node.js是一個完全獨立的程式環境,完全不依賴其他伺服器,這對於熟悉php、.net這些環境的開發者,可能會有點陌生。不過也因為這樣,開發者可以完全掌控整個伺服器的運作過程,也可以激發很多創意。

目前node.js 已經可以發佈到 9.4.0 , 如果要使用最新的版本...可以這樣做

Install Nodejs using NVM(建議這種方式)

(1)To install nvm, run the following command:
# curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

The above command will clone the nvm repository to ~/.nvm and add the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

因為有寫入.bashrc 等PATH , 所以使用前需要 Restart your Terminal once before start using NVM.

To verify whether NVM is installed or not, run:
# command -v nvm

Sample output:
nvm

Now, we can install Nodejs and npm.

First, run the following command to view the list of available Nodejs versions:

# nvm ls-remote

Sample output:
[...]
v8.8.1
v8.9.0 (LTS: Carbon)
v8.9.1 (LTS: Carbon)
v8.9.2 (LTS: Carbon)
v8.9.3 (LTS: Carbon)
v8.9.4 (Latest LTS: Carbon)
v9.0.0
v9.1.0
v9.2.0
v9.2.1
v9.3.0
v9.4.0

To install/update to the most recent Nodejs version, just run:

$ nvm install node

Alternatively, you can run the following to install any Nodejs version of your choice.

For example, to install Nodejs v9.3.0, run:

# nvm install v9.3.0

QUOTE:


Sample output:

Downloading and installing node v9.3.0...
Downloading https://nodejs.org/dist/v9.3.0/node-v9.3.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v9.3.0 (npm v5.6.0)
Creating default alias: default -> v9.3.0


Similarly, you can install any number of versions you want.
To view the list of installed Nodejs versions, run:

# nvm list

QUOTE:


Sample output:

-> v10.0.0
default -> node (-> v10.0.0)
node -> stable (-> v10.0.0) (default)
stable -> 10.0 (-> v10.0.0) (default)
iojs -> N/A (default)
lts/* -> lts/carbon (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.14.1 (-> N/A)
lts/carbon -> v8.11.1 (-> N/A)

As you see in the above output, I have installed v10.0.0.

If you have installed more than one version, you can switch between different Nodejs versions as below.

# nvm use node

Or you can just run it to be more specific:

# nvm run node v9.3.0

To set a particular Nodejs version as the default, run:

# nvm alias default v9.3.0

Sample output would be:

default -> v9.3.0

To remove the particular Nodejs version, run:

# nvm uninstall v9.3.0

Sample output:

Uninstalled node v9.3.0

To view the currently installed and default Nodejs version, run:

# node -v
v10.0.0

Check npm version:
# npm -v
5.6.0

這樣就大功告成啦~

參考資料:
https://www.ostechnix.com/install-node-js-linux/