Use Nvm to Install Node.js

Node.js is an open-source, cross-platform, JavaScript run-time environment that executes JavaScript code outside of a browser.

Node.js:https://nodejs.org

Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user’s web browser.

Install or Update NVM

Install/Update Use Script

1
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

Note: If the environment variable $XDG_CONFIG_HOME is present, it will place the nvm files there.

1
2
3
export NVM_DIR="${XDG_CONFIG_HOME/:-$HOME/.}nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use # using nvm until manually run `nvm use default`.

After running the install script, close current terminal and open a new terminal,

Install/Update Use Git

  • Install
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    :~$ export NVM_DIR="$HOME/.nvm" && (
    git clone https://github.com/nvm-sh/nvm.git "$NVM_DIR"
    cd "$NVM_DIR"
    git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
    ) && \. "$NVM_DIR/nvm.sh"
    ...
    ...
    # add these lines to `~/.bashrc`, `~/.profile`, or `~/.zshrc` file
    # to have it automatically sourced upon login
    :~/.nvm$ cat >> ~/.bashrc << EOF
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    # [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
    EOF
  • Update
    1
    2
    3
    4
    5
    (
    cd "$NVM_DIR"
    git fetch --tags origin
    git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
    ) && \. "$NVM_DIR/nvm.sh"

Install Node Use NVM

download/compile/install the latest release of Node

1
2
:~$ cd ~/.nvm
:~/.nvm$ nvm install node

List Available Node Versions

1
nvm ls-remote

Install a specific version of Node

1
nvm install 11.14

Use Command Npm of Node

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
:~$ nvm use default
:~$ npm version
{
npm: '6.10.3',
ares: '1.15.0',
brotli: '1.0.7',
cldr: '35.1',
http_parser: '2.8.0',
icu: '64.2',
llhttp: '1.1.4',
modules: '72',
napi: '4',
nghttp2: '1.39.2',
node: '12.10.0',
openssl: '1.1.1c',
tz: '2019a',
unicode: '12.1',
uv: '1.31.0',
v8: '7.6.303.29-node.16',
zlib: '1.2.11'
}

Global Packages

  • Update all global packages
    1
    2
    3
    4
    5
    :~$ cd "$NVM_DIR"
    # Method One
    :~/.nvm$ npm -g install
    # Method Two
    :~/.nvm$ npm -g update
  • lookup which packages need to be updated
    1
    2
    3
    4
    :~$ npm -g outdated
    Package Current Wanted Latest Location
    appium 1.5.2 1.5.3 1.5.3
    bower 1.7.0 1.7.9 1.7.9
  • Update the global program to specify version
    1
    2
    3
    4
    5
    6
    :~$ cd "$NVM_DIR"
    # Method One
    :~/.nvm$ npm -g install [email protected]
    :~/.nvm$ npm -g install npm@next
    # Method Two
    :~/.nvm$ npm -g update [email protected]
  • uninstall global package
    1
    2
    :~$ cd "$NVM_DIR"
    :~/.nvm$ npm -g uninstall <package name>
  • Audit security of the global packages
    1
    2
    :~/.nvm$ npm audit
    :~/.nvm$ npm audit fix

Local Packages

  • Update all local packages
    1
    2
    3
    4
    5
    :~$ cd ~/.hexo
    # Method One
    :~/.hexo$ npm install
    # Method Two
    :~/.hexo$ npm update
  • lookup which local packages need to be updated
    1
    2
    3
    :~/.hexo$ npm outdated
    Package Current Wanted Latest Location
    requirejs 2.1.22 2.2.0 2.2.0
  • Update the local packages to specify version
    1
    2
    3
    4
    5
    :~$ cd "$NVM_DIR"
    # Method One
    :~/.hexo$ npm install [email protected]
    # Method Two
    :~/.hexo$ npm install requirejs@next
  • uninstall the local package
    1
    :~/.hexo$ npm uninstall <package name>
  • Audit security of the local packages
    1
    2
    :~/.hexo$ npm audit
    :~/.hexo$ npm audit fix

Prefence:
Github_NVM