linux 安裝 npm nodejs

轉自 https://www.cnblogs.com/scajy/p/13218721.html

一、環(huán)境準備

1.1 查看系統(tǒng)環(huán)境

[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

提示:這里我們發(fā)現(xiàn)安裝版本有點低,我們需要在下面升級一下版本

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到穩(wěn)定版

[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、驗證是否是最新穩(wěn)定版

[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:執(zhí)行的工具
    install:執(zhí)行安裝命令

四、安裝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 監(jiān)視每個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.
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市尽超,隨后出現(xiàn)的幾起案子官撼,更是在濱河造成了極大的恐慌,老刑警劉巖似谁,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件傲绣,死亡現(xiàn)場離奇詭異,居然都是意外死亡棘脐,警方通過查閱死者的電腦和手機斜筐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門龙致,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蛀缝,“玉大人,你說我怎么就攤上這事目代∏海” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵榛了,是天一觀的道長在讶。 經常有香客問我,道長霜大,這世上最難降的妖魔是什么构哺? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上曙强,老公的妹妹穿的比我還像新娘残拐。我一直安慰自己,他們只是感情好碟嘴,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布溪食。 她就那樣靜靜地躺著,像睡著了一般娜扇。 火紅的嫁衣襯著肌膚如雪错沃。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天雀瓢,我揣著相機與錄音枢析,去河邊找鬼。 笑死刃麸,一個胖子當著我的面吹牛登疗,可吹牛的內容都是我干的。 我是一名探鬼主播嫌蚤,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼辐益,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了脱吱?” 一聲冷哼從身側響起智政,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎箱蝠,沒想到半個月后续捂,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡宦搬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年牙瓢,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片间校。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡矾克,死狀恐怖,靈堂內的尸體忽然破棺而出憔足,到底是詐尸還是另有隱情胁附,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布滓彰,位于F島的核電站控妻,受9級特大地震影響,放射性物質發(fā)生泄漏揭绑。R本人自食惡果不足惜弓候,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧菇存,春花似錦彰居、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至毕籽,卻和暖如春抬闯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背关筒。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工溶握, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蒸播。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓睡榆,卻偏偏與公主長得像,于是被迫代替她去往敵國和親袍榆。 傳聞我的和親對象是個殘疾皇子胀屿,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內容