Installing nodejs environment using nvm under Centos7

Posted by maxic0 on Sat, 15 Jan 2022 12:23:14 +0100

Install nvm

nvm is an open source tool that can switch and manage the nodejs version. You can see the warehouse on GitHub. Visit the GitHub of nvm, and you can see the relevant installation scripts according to the introduction, as follows:

image-20200811150552884

"Execute installation script"

Execute curl - O- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh |Bash is as follows:

[root@dev ~]# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13527  100 13527    0     0   8435      0  0:00:01  0:00:01 --:--:--  8438
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 290, done.
remote: Counting objects: 100% (290/290), done.
remote: Compressing objects: 100% (257/257), done.
remote: Total 290 (delta 35), reused 97 (delta 20), pack-reused 0
Receiving objects: 100% (290/290), 163.27 KiB | 3.00 KiB/s, done.
Resolving deltas: 100% (35/35), done.
=> Compressing and cleaning up git repository

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
[root@dev ~]#

According to the prompt, you can see that the script has added the contents of relevant environment variables to / root / In the bashrc file, let's take a look, as follows:

image-20200811152141839

"Set the environment variable to take effect"

[root@dev ~]# source /root/.bashrc
[root@dev ~]# nvm -v

Node Version Manager (v0.35.3)

Note: <version> refers to any version-like string nvm understands. This includes:
  - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
  - default (built-in) aliases: node, stable, unstable, iojs, system
  - custom aliases you define with `nvm alias foo`

 Any options that produce colorized output should respect the `--no-colors` option.

Installing nodejs using nvm

First, query the available nodejs versions:

$ nvm ls-remote
# perhaps
$ nvm ls available

Install the latest nodejs version:

$ nvm install node # "node" is an alias for the latest version

Or install according to the version number:

$ nvm install 6.14.4 # or 10.10.0, 8.9.1, etc

Install the latest version as follows:

[root@dev ~]# nvm install node
Downloading and installing node v14.7.0...
Downloading https://nodejs.org/dist/v14.7.0/node-v14.7.0-linux-x64.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.7.0 (npm v6.14.7)
Creating default alias: default -> node (-> v14.7.0)
[root@dev ~]#

Switch to use nodejs version

Directly use the latest node version:

[root@dev ~]# nvm use node
Now using node v14.7.0 (npm v6.14.7)

Alternatively, you can switch to a specific version:

# nvm use 6.16.0

View the version of nodejs

[root@dev ~]# node -v
v14.7.0
[root@dev ~]#

Install nrm

When using npm to download the image, the foreign image address is used by default. When using the foreign address to download the software in China, there must be a slow network process.

At this time, we should consider switching the domestic download image. However, there is also a problem. Switching the domestic image is sometimes troublesome. At this time, you can use nrm to switch the image address.

Role of nrm

Function: it provides some of the most commonly used image addresses of NPM packages, enabling us to quickly switch the server address when installing packages; What is mirroring: the original package only existed in foreign NPM servers at the beginning, but it is often inaccessible due to network reasons. At this time, we can create an NPM server exactly the same as the official website in China, but the data is taken from others. In addition, the use method is exactly the same;

  1. Run npm i nrm -g global installation nrm package;
  2. Use nrm ls to view all available image source addresses and the currently used image source addresses;
  3. Use nrm use npm or nrm use taobao to switch different image source addresses;

 note: nrm simply provides several common download package URL addresses and allows us to easily switch between these addresses. However, each time we pack, the packaging tool we use is npm. "

Then let's demonstrate the process of installation and use.

1. Global installation nrm

Run npm i nrm -g and install as follows:

# Global installation
[root@dev ~]# npm i nrm -g
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated coffee-script@1.7.1: CoffeeScript on NPM has moved to "coffeescript" (no hyphen)
npm WARN deprecated mkdirp@0.3.5: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
/root/.nvm/versions/node/v14.7.0/bin/nrm -> /root/.nvm/versions/node/v14.7.0/lib/node_modules/nrm/cli.js
+ nrm@1.2.1
added 493 packages from 887 contributors in 57.446s
[root@dev ~]#

# View mirror source
[root@dev ~]# nrm ls

* npm -------- https://registry.npmjs.org/
  yarn ------- https://registry.yarnpkg.com/
  cnpm ------- http://r.cnpmjs.org/
  taobao ----- https://registry.npm.taobao.org/
  nj --------- https://registry.nodejitsu.com/
  npmMirror -- https://skimdb.npmjs.com/registry/
  edunpm ----- http://registry.enpmjs.org/

[root@dev ~]#

2. View the current source nrm current

$ nrm current
npm

3. Switch source

nrm use <registry>

Where registry is the source name.

For example, switch to taobao source

nrm use taobao

4. Add source

nrm add <registry> <url>

Where registry is the source name and url is the source address.

For example, add a company private NPM source with the source address: http://192.168.10.1:8888/repository/npm-public /, the source name is cpm (optional).

nrm add cpm http://192.168.10.1:8888/repository/npm-public/

5. Delete source

nrm del <registry>

Where registry is the source name.

For example, delete the cpm source you just added

nrm del cpm

6. Test source speed (i.e. response time)

nrm test <registry>

Where registry is the source name.

For example, test the response time of official sources and Taobao sources

nrm test npm

7. Install cnpm

npm i cnpm -g

Note: cnpm here is not the image address of cnpm after nrm is installed above, but an installation tool.