ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

ubuntu 16.04安装node和npm的方法详解(npm官网推荐)

2019-08-31 17:39:46  阅读:246  来源: 互联网

标签:npm node 16.04 Node install js 安装


安装node和npm是前端开发必备的环节,但在这一环节上潜藏一些坑,稍有不慎可能会造成环境配置问题。网上的方法林林总总,纷繁复杂,但其实npm官网上已经给出了最佳的安装方式:
Downloading and installing Node.js and npm

To publish and install packages to and from the public npm registry or your company’s npm Enterprise registry, you must install Node.js and the npm command line interface using either a Node version manager or a Node installer. We strongly recommend using a Node version manager to install Node.js and npm. We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages globally.

“我们强烈建议使用Node版本管理器来安装Node.js和npm。
我们不建议使用Node安装程序,因为Node安装过程会在具有本地权限的目录中安装npm,并且在全局运行npm软件包时可能会导致权限错误。”

后面一句话指的是在全局安装npm包时的EACCES permissions errors 错误,对于该错误,npm官网也给出解决方案:
Resolving EACCES permissions errors when installing packages globally

使用NVM安装(npm官网推荐)

Node Version Manager

  1. 安装NVM
    $ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
    
  2. 检验安装效果
    $ command -v nvm
    
    在这里插入图片描述
    出现上图所示结果即代表nvm安装成功。
  3. 安装node
    $ nvm install node   # "node" is an alias for the latest version
    
    也可以安装指定版本
    $ nvm install 10.16.3
    
    在这里插入图片描述
    出现上图所示结果即代表node和npm安装成功。

使用n模块安装(npm官网推荐)

n – Interactively Manage Your Node.js Versions
n模块是Node.js的版本管理工具,使用它可以方便地管理多个Node版本。
操作简便,容易上手,但有一些坑。

  • 如果已经有node,可以使用npm安装

    $ npm install -g n
    
  • 从github下载编译

    $ make install
    
  • 第三方安装

    $ curl -L https://git.io/n-install | bash
    

    在这种方式下需要刷新环境变量

    $ source /home/hellocrease/.bashrc   #我使用的账号名称为hellocrease,读者请改成自己的名字
    

    n-install sets both PREFIX and N_PREFIX to $HOME/n, installs n to $HOME/n/bin, modifies the initialization files of supported shells to export N_PREFIX and add $HOME/n/bin to the PATH, and installs the latest LTS node version.

    As a result, both n itself and all node versions it manages are hosted inside a single, optionally configurable directory, which you can later remove with the included n-uninstall script. n-update updates n itself to the latest version. See the n-install repo for more details.

使用n模块安装node和npm也会造成EACCES permissions errors (即需要使用sudo来安装npm包)
官网上给出了两种解决方法:

To avoid requiring sudo for n and npm global installs, it is suggested you either install to your home directory using N_PREFIX, or take ownership of the system directories.

  1. 在安装时设置N_PREFIX选项,将其安装到自定义位置
  2. 获得系统文件的权限
    # make cache folder (if missing) and take ownership
    $ sudo mkdir -p /usr/local/n
    $ sudo chown -R $(whoami) /usr/local/n
    
    # take ownership of node install destination folders
    $ sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
    

注:使用第三种方式安装后,如果需要删除,需要修改环境变量,将N_PREFIX那一行注释掉。

$ vi /home/hellocrease/.bashrc  #我使用的账号名称为hellocrease,读者请改成自己的名字

在这里插入图片描述
否则会出现错误:
在这里插入图片描述

使用PPA方式安装

Node.js Binary Distributions

  1. 下载Node.js安装包
    $ curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
    
  2. 安装Node.js
    $ sudo apt-get install -y nodejs
    
  3. 安装Node.js版本管理模块n
    $ sudo npm install -g n
    
  4. 升级Node.js
    $ sudo n stable // 官方稳定版
    $ sudo n latest // 官方最新版
    $ sudo n lts // 官方最新LTS版本
    
  5. 查看版本号
    $ npm -v
    $ node -v
    
    在这里插入图片描述
    如图,Node.js和npm已经安装成功。

标签:npm,node,16.04,Node,install,js,安装
来源: https://blog.csdn.net/Liut2016/article/details/100174325

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有