美文网首页
linux 安装 npm nodejs

linux 安装 npm nodejs

作者: Joncc | 来源:发表于2020-12-21 18:00 被阅读0次

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

一、环境准备

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.

相关文章

网友评论

      本文标题:linux 安装 npm nodejs

      本文链接:https://www.haomeiwen.com/subject/wzbrnktx.html