centos7 yum 安裝nodejs、npm、cnpm、pm2、yarn


一、環境准備

1.1 查看系統環境

[root@localhost ~]#  cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@localhost ~]# uname -m
x86_64
[root@localhost ~]# uname -r
3.10.0-862.el7.x86_64

1.2 關閉防火牆和selinux

1.2.1 關閉防火牆

[root@localhost ~]# /bin/systemctl stop firewalld
[root@localhost ~]# /bin/systemctl disable firewalld

1.2.2 關閉selinux

[root@localhost ~]# getenforce                 #查看selinux是否開啟
Enforcing                                         #enforcing表示selinux開啟的,
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g'  /etc/selinux/config      #修改配置文件,需要重啟才后永久關閉
[root@localhost ~]# setenforce 0      #臨時關閉selinx
[root@localhost ~]# getenforce      #查看是否關閉
Disabled

二、安裝epel源

   [root@localhost ~]# yum install epel-release -y

三、安裝nodejs和npm

3.1 安裝nodejs和npm

[root@localhost ~]# yum install nodejs -y
已加載插件:fastestmirror
Determining fastest mirrors
epel/x86_64/metalink                                                                                                                                          | 6.3 kB  00:00:05     
 * base: mirrors.aliyun.com
 * city-fan.org: nervion.us.es
 * epel: hkg.mirror.rackspace.com
 * extras: mirrors.aliyun.com
 * updates: mirror-hk.koddos.net
base                                                                                                                                                          | 3.6 kB  00:00:00     
city-fan.org                                                                                                                                                  | 3.0 kB  00:00:00     
epel                                                                                                                                                          | 4.7 kB  00:00:00     
extras                                                                                                                                                        | 2.9 kB  00:00:00     
updates                                                                                                                                                       | 2.9 kB  00:00:00     
(1/8): base/7/x86_64/group_gz                                                                                                                                 | 153 kB  00:00:00     
(2/8): base/7/x86_64/primary_db                                                                                                                               | 6.1 MB  00:00:03     
(3/8): extras/7/x86_64/primary_db                                                                                                                             | 194 kB  00:00:00     
(4/8): epel/x86_64/group_gz                                                                                                                                   |  95 kB  00:00:03     
(5/8): updates/7/x86_64/primary_db                                                                                                                            | 3.0 MB  00:00:02     
(6/8): city-fan.org/7/primary_db                                                                                                                              | 508 kB  00:00:13     
(7/8): epel/x86_64/updateinfo                                                                                                                                 | 1.0 MB  00:00:32     
(8/8): epel/x86_64/primary_db                                                                                                                                 | 6.8 MB  00:03:40     
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 nodejs.x86_64.1.6.17.1-1.el7 將被 安裝
--> 正在處理依賴關系 npm = 1:3.10.10-1.6.17.1.1.el7,它被軟件包 1:nodejs-6.17.1-1.el7.x86_64 需要
--> 正在處理依賴關系 libuv >= 1:1.9.1,它被軟件包 1:nodejs-6.17.1-1.el7.x86_64 需要
--> 正在處理依賴關系 libuv.so.1()(64bit),它被軟件包 1:nodejs-6.17.1-1.el7.x86_64 需要
--> 正在檢查事務
---> 軟件包 libuv.x86_64.1.1.38.0-2.el7 將被 安裝
---> 軟件包 npm.x86_64.1.3.10.10-1.6.17.1.1.el7 將被 安裝
--> 解決依賴關系完成

依賴關系解決

=====================================================================================================================================================================================
 Package                                架構                                   版本                                                       源                                    大小
=====================================================================================================================================================================================
正在安裝:
 nodejs                                 x86_64                                 1:6.17.1-1.el7                                             epel                                 4.7 M
為依賴而安裝:
 libuv                                  x86_64                                 1:1.38.0-2.el7                                             epel                                 148 k
 npm                                    x86_64                                 1:3.10.10-1.6.17.1.1.el7                                   epel                                 2.5 M

事務概要
=====================================================================================================================================================================================
安裝  1 軟件包 (+2 依賴軟件包)

總下載量:7.4 M
安裝大小:27 M
Downloading packages:
(1/3): npm-3.10.10-1.6.17.1.1.el7.x86_64.rpm                                                                                                                  | 2.5 MB  00:00:02     
(2/3): libuv-1.38.0-2.el7.x86_64.rpm                                                                                                                          | 148 kB  00:00:10     
nodejs-6.17.1-1.el7.x86_64.rpm FAILED                                           [=======================-                                         ]  1.3 B/s | 2.7 MB 1064:06:38 ETA 
https://mirror.yer.az/fedora-epel/7/x86_64/Packages/n/nodejs-6.17.1-1.el7.x86_64.rpm: [Errno 12] Timeout on https://mirror.yer.az/fedora-epel/7/x86_64/Packages/n/nodejs-6.17.1-1.el7.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
正在嘗試其它鏡像。
(3/3): nodejs-6.17.1-1.el7.x86_64.rpm                                                                                                                         | 4.7 MB  00:00:03     
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
總計                                                                                                                                                  98 kB/s | 7.4 MB  00:01:16     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : 1:libuv-1.38.0-2.el7.x86_64                                                                                                                                      1/3 
  正在安裝    : 1:nodejs-6.17.1-1.el7.x86_64                                                                                                                                     2/3 
  正在安裝    : 1:npm-3.10.10-1.6.17.1.1.el7.x86_64                                                                                                                              3/3 
  驗證中      : 1:npm-3.10.10-1.6.17.1.1.el7.x86_64                                                                                                                              1/3 
  驗證中      : 1:nodejs-6.17.1-1.el7.x86_64                                                                                                                                     2/3 
  驗證中      : 1:libuv-1.38.0-2.el7.x86_64                                                                                                                                      3/3 

已安裝:
  nodejs.x86_64 1:6.17.1-1.el7                                                                                                                                                       

作為依賴被安裝:
  libuv.x86_64 1:1.38.0-2.el7                                                           npm.x86_64 1:3.10.10-1.6.17.1.1.el7                                                          

完畢!

3.2、查看安裝node

[root@localhost ~]# node -v
v6.17.1
[root@localhost ~]# npm -v
3.10.10

提示:這里我們發現安裝版本有點低,我們需要在下面升級一下版本

3.3、設置淘寶鏡像和安裝淘寶cnpm

npm config set registry https://registry.npm.taobao.org
npm install -g cnpm --registry=https://registry.npm.taobao.org

3.4、安裝升級node版本工具

[root@localhost ~]# npm install -g n
/usr/bin/n -> /usr/lib/node_modules/n/bin/n
/usr/lib
└── n@6.5.1 

3.5、驗證n工具能否使用,並查看幫助怎么使用

[root@localhost ~]# n -h

Usage: n [options] [COMMAND] [args]

Commands:

  n                              Display downloaded node versions and install selection
  n latest                       Install the latest node release (downloading if necessary)
  n lts                          Install the latest LTS node release (downloading if necessary)
  n <version>                    Install node <version> (downloading if necessary)
  n run <version> [args ...]     Execute downloaded node <version> with [args ...]
  n which <version>              Output path for downloaded node <version>
  n exec <vers> <cmd> [args...]  Execute command with modified PATH, so downloaded node <version> and npm first
  n rm <version ...>             Remove the given downloaded version(s)
  n prune                        Remove all downloaded versions except the installed version
  n --latest                     Output the latest node version available
  n --lts                        Output the latest LTS node version available
  n ls                           Output downloaded versions
  n ls-remote [version]          Output matching versions available for download
  n uninstall                    Remove the installed node and npm

Options:

  -V, --version         Output version of n
  -h, --help            Display help information
  -p, --preserve        Preserve npm and npx during install of node (requires rsync)
  -q, --quiet           Disable curl output (if available)
  -d, --download        Download only
  -a, --arch            Override system architecture
  --all                 ls-remote displays all matches instead of last 20
  --insecure            Turn off certificate checking for https requests (may be needed from behind a proxy server)
  --use-xz/--no-use-xz  Override automatic detection of xz support and enable/disable use of xz compressed node downloads.

Aliases:

  which: bin
  run: use, as
  ls: list
  lsr: ls-remote
  rm: -
  lts: stable
  latest: current

Versions:

  Numeric version numbers can be complete or incomplete, with an optional leading 'v'.
  Versions can also be specified by label, or codename,
  and other downloadable releases by <remote-folder>/<version>

    4.9.1, 8, v6.1    Numeric versions
    lts               Newest Long Term Support official release
    latest, current   Newest official release
    auto              Read version from .n-node-version
    boron, carbon     Codenames for release streams
    and nightly, chakracore-release/latest, rc/10 et al	

3.6 升級node到穩定版

[root@localhost ~]# n stable

  installing : node-v12.18.2
       mkdir : /usr/local/n/versions/node/12.18.2
       fetch : https://nodejs.org/dist/v12.18.2/node-v12.18.2-linux-x64.tar.xz
   installed : v12.18.2 (with npm 6.14.5)

Note: the node command changed location and the old location may be remembered in your current shell.
         old : /usr/bin/node
         new : /usr/local/bin/node
To reset the command location hash either start a new shell, or execute PATH="$PATH"

3.7、升級npm到最新

[root@localhost ~]# npm install -g npm
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
/usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
/usr/local/lib
└── npm@6.14.5 

3.8、驗證是否是最新穩定版

[root@localhost ~]# node -v
v12.18.2
[root@localhost ~]# npm -v
6.14.5

3.9、n工具指定node版本,命令打包編譯

n exec 12 cnpm install

注釋:
	n:為指定使用的工具
	exec:命令使用的方法
	12:為node版本
	cnpm:執行的工具
	install:執行安裝命令

四、安裝pm2工具

4.1、安裝pm2工具

[root@localhost ~]# npm install -g pm2
/usr/local/bin/pm2 -> /usr/local/lib/node_modules/pm2/bin/pm2
/usr/local/bin/pm2-dev -> /usr/local/lib/node_modules/pm2/bin/pm2-dev
/usr/local/bin/pm2-docker -> /usr/local/lib/node_modules/pm2/bin/pm2-docker
/usr/local/bin/pm2-runtime -> /usr/local/lib/node_modules/pm2/bin/pm2-runtime
npm WARN ws@7.2.5 requires a peer of bufferutil@^4.0.1 but none is installed. You must install peer dependencies yourself.
npm WARN ws@7.2.5 requires a peer of utf-8-validate@^5.0.2 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.3 (node_modules/pm2/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ pm2@4.4.0
added 185 packages from 191 contributors in 14.145s

4.2、pm2工具幫助使用

pm2 start app.js  啟動app項目

pm2 list 列出由pm2管理的所有進程信息,還會顯示一個進程會被啟動多少次,因為沒處理的異常。

pm2 monit 監視每個node進程的CPU和內存的使用情況

pm2 logs 顯示所有進程日志

pm2 stop all 停止所有進程

pm2 restart all 重啟所有進程

pm2 reload all 0秒停機重載進程 (用於 NETWORKED 進程)

pm2 stop 0 停止指定的進程

pm2 restart 0 重啟指定的進程

pm2 startup 產生 init 腳本 保持進程活着

pm2 web 運行健壯的 computer API endpoint (http://localhost:9615)

pm2 delete 0 殺死指定的進程

pm2 delete all 殺死全部進程

五、安裝yarn工具

5.1、安裝yarn工具

[root@localhost ~]# npm install -g yarn
/usr/local/bin/yarn -> /usr/local/lib/node_modules/yarn/bin/yarn.js
/usr/local/bin/yarnpkg -> /usr/local/lib/node_modules/yarn/bin/yarn.js
+ yarn@1.22.4
added 1 package in 1.189s	

5.2、查看yarn工具幫助

[root@localhost ~]# yarn -h

  Usage: yarn [command] [flags]

  Displays help information.

  Options:

    --cache-folder <path>               specify a custom folder that must be used to store the yarn cache
    --check-files                       install will verify file tree of packages for consistency
    --cwd <cwd>                         working directory to use (default: /root)
    --disable-pnp                       disable the Plug'n'Play installation
    --emoji [bool]                      enable emoji in output (default: false)
    --enable-pnp, --pnp                 enable the Plug'n'Play installation
    --flat                              only allow one version of a package
    --focus                             Focus on a single workspace by installing remote copies of its sibling workspaces.
    --force                             install and build packages even if they were built before, overwrite lockfile
    --frozen-lockfile                   don't generate a lockfile and fail if an update is needed
    --global-folder <path>              specify a custom folder to store global packages
    --har                               save HAR output of network traffic
    --https-proxy <host>                
    --ignore-engines                    ignore engines check
    --ignore-optional                   ignore optional dependencies
    --ignore-platform                   ignore platform checks
    --ignore-scripts                    don't run lifecycle scripts
    --json                              format Yarn log messages as lines of JSON (see jsonlines.org)
    --link-duplicates                   create hardlinks to the repeated modules in node_modules
    --link-folder <path>                specify a custom folder to store global links
    --modules-folder <path>             rather than installing modules into the node_modules folder relative to the cwd, output them here
    --mutex <type>[:specifier]          use a mutex to ensure only one yarn instance is executing
    --network-concurrency <number>      maximum number of concurrent network requests
    --network-timeout <milliseconds>    TCP timeout for network requests
    --no-bin-links                      don't generate bin links when setting up packages
    --no-default-rc                     prevent Yarn from automatically detecting yarnrc and npmrc files
    --no-lockfile                       don't read or generate a lockfile
    --non-interactive                   do not show interactive prompts
    --no-node-version-check             do not warn when using a potentially unsupported Node version
    --no-progress                       disable progress bar
    --offline                           trigger an error if any required dependencies are not available in local cache
    --otp <otpcode>                     one-time password for two factor authentication
    --prefer-offline                    use network only if dependencies are not available in local cache
    --preferred-cache-folder <path>     specify a custom folder to store the yarn cache if possible
    --prod, --production [prod]         
    --proxy <host>                      
    --pure-lockfile                     don't generate a lockfile
    --registry <url>                    override configuration registry
    -s, --silent                        skip Yarn console logs, other types of logs (script output) will be printed
    --scripts-prepend-node-path [bool]  prepend the node executable dir to the PATH in scripts
    --skip-integrity-check              run install without checking if node_modules is installed
    --strict-semver                     
    --update-checksums                  update package checksums from current repository
    --use-yarnrc <path>                 specifies a yarnrc file that Yarn should use (.yarnrc only, not .npmrc) (default: )
    -v, --version                       output the version number
    --verbose                           output verbose messages on internal operations
    -h, --help                          output usage information
  Commands:
    - access
    - add
    - audit
    - autoclean
    - bin
    - cache
    - check
    - config
    - create
    - exec
    - generate-lock-entry / generateLockEntry
    - global
    - help
    - import
    - info
    - init
    - install
    - licenses
    - link
    - list
    - login
    - logout
    - node
    - outdated
    - owner
    - pack
    - policies
    - publish
    - remove
    - run
    - tag
    - team
    - unlink
    - unplug
    - upgrade
    - upgrade-interactive / upgradeInteractive
    - version
    - versions
    - why
    - workspace
    - workspaces

  Run `yarn help COMMAND` for more information on specific commands.
  Visit https://yarnpkg.com/en/docs/cli/ to learn more about Yarn.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM