8

Centos下安装Node运行环境

 2 years ago
source link: https://baozi.fun/2020/09/09/centos-install-node
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

如果要运行一个Express服务,就必须要安装一个Node环境,本篇文章就来讲讲如何在Centos7下安装Node环境。

安装Node

其实安装Node环境很简单,我们通过 yum 工具就可以安装

安装EPEL

EPEL(Extra Packages for Enterprise Linux)企业版Linux的额外软件包,是Fedora小组维护的一个软件仓库项目,为RHEL/CentOS提供他们默认不提供的软件包。

yum install epel-release

安装完后,就可以使用yum命令安装nodejs了,安装的一般会是6.x的版本,并且会将npm(3.x)作为依赖包一起安装

yum install nodejs

安装完成后,验证是否正确的安装,node -v,如果输出如下版本信息,说明成功安装

v6.13.3

问题来了,现在nodejs发的版本比较快,有些新的框架需要node的新版本,那如何升级。到现在,node的最新版本是10.4.1,那么,下面介绍如何升级nodejs。

因为我之前已经安装过了,所以本文只有步骤了。

升级Nodejs到最新版本

为了方便管理,我们使用了一个版本管理器:n

安装 n

npm install -g n

安装Nodejs最新版本

n latest 

安装指定版本

n 8.11.3  

查看通过n安装的node版本

n list

n list

切换Nodejs版本

n

switch node version

好了,我们现在来看看 Node 的版本:

node -v

node -v

切换版本失败的处理办法

查看 node 当前安装路径

which node
/usr/local/bin/node #举个例子

而 n 默认安装路径是 /usr/local,若你的 node 不是在此路径下,n 切换版本就不能把bin、lib、include、share 复制该路径中,所以我们必须通过N_PREFIX变量来修改 n 的默认node安装路径。
编辑环境配置文件:

vim ~/.bash_profile

将下面两行代码插入到文件末尾:

export N_PREFIX=/usr/local #node实际安装位置
export PATH=$N_PREFIX/bin:$PATH

:wq保存退出

执行source使修改生效。

source ~/.bash_profile

这时候再查看node -v发现版本切换成功了。

设置淘宝镜像

现在我们已经安装了 node 和 npm了,但是使用npm来安装依赖,实在是太慢了,所以我们设置一个淘宝镜像。

npm config set registry https://registry.npm.taobao.org
npm config get registry

npm config get registry

通过cnpm使用

cnpm 是国内很好的一个包管理工具,安装依赖超级快。

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK