Centos 安装nodejs

Centos 安装nodejs

此次通过nvm安装方式进行安装

nvm下载地址https://github.com/cnpm/nvm/

1.安装nvm

git clone https://github.com/cnpm/nvm.git ~/.nvm 
cd ~/.nvm 
git checkout `git describe --abbrev=0 --tags`
//运行下面命令
. ~/.nvm/install.sh

下面是nvm的一些用法

[root@ovensbb ~]# nvm -help

Node Version Manager

Usage:
  nvm help                              Show this message
  nvm --version                         Print out the latest released version of nvm
  nvm install [-s] <version>            Download and install a <version>, [-s] from source. Uses .nvmrc if available
  nvm uninstall <version>               Uninstall a version
  nvm use <version>                     Modify PATH to use <version>. Uses .nvmrc if available
  nvm run <version> [<args>]            Run <version> with <args> as arguments. Uses .nvmrc if available for <version>
  nvm current                           Display currently activated version
  nvm ls                                List installed versions
  nvm ls <version>                      List versions matching a given description
  nvm ls-remote                         List remote versions available for install
  nvm deactivate                        Undo effects of `nvm` on current shell
  nvm alias [<pattern>]                 Show all aliases beginning with <pattern>
  nvm alias <name> <version>            Set an alias named <name> pointing to <version>
  nvm unalias <name>                    Deletes the alias named <name>
  nvm reinstall-packages <version>      Reinstall global `npm` packages contained in <version> to current version
  nvm unload                            Unload `nvm` from shell
  nvm which [<version>]                 Display path to installed node version. Uses .nvmrc if available

Example:
  nvm install v0.10.32                  Install a specific version number
  nvm use 0.10                          Use the latest available 0.10.x release
  nvm run 0.10.32 app.js                Run app.js using node v0.10.32
  nvm exec 0.10.32 node app.js          Run `node app.js` with the PATH pointing to node v0.10.32
  nvm alias default 0.10.32             Set default node version on a shell

Note:
  to remove, delete, or uninstall nvm - just remove ~/.nvm, ~/.npm, and ~/.bower folders

2.查看node版本列表

[root@ovensbb ~]# nvm ls-remote
            ...
       v0.12.13
       v0.12.14
       v0.12.15
            ...
         v4.4.6
         v4.4.7
         v4.5.0(稳定版)
            ...
         v5.4.1
         v5.5.0
            ...
         v6.2.2
         v6.3.0
         v6.3.1
         v6.4.0
         v6.5.0
         v6.6.0(开发版)
            ...

3.安装最新开发版并检查版本

[root@ovensbb ~]# nvm install v6.6.0
######################################################################## 100.0%
Checksums empty
Now using node v6.9.0
[root@ovensbb ~]# node -v
v6.9.0

4.设置默认node

1
nvm alias default v6.9.0

下载源码包编译安装

1.安装编译环境

sudo yum install gcc gcc-c++ wget

2.下载源码包

wget https://nodejs.org/dist/v4.5.0/node-v4.5.0.tar.gz

3.解压并安装

tar -zvxf node-v4.5.0.tar.gz  
cd node-v4.5.0
make && make install
//检查版本
node -v

通过express创建项目

安装express

1、安装express,使用npm install express -g全局安装,不能通过 express -e nodejs-001创建项目会提示“bash: express: 未找到命令…”

2、安装express时增加generator参数:npm install -g express-generator才可以,express 已经把命令行工具分离出来了;
原因:原先的express带cli, 现在把cli拆成了单独的express-generator包. 原先的express运行生成的项目是 node app.js, 因为httpserver相关代码都在app.js里, 现在这部分代码移到了项目目录的bin/www下面, app.js 只保留实现app的逻辑代码, 你需要去运行那个bin/www。 只是很单纯的细化应用和包依赖的版本变更。

建立express工程,启动项目

1、创建工程express -e nodejs-001;

2、创建工程时候会提示需要安装依赖 cd nodejs-001 && npm install;

3、启动项目npm start,通过浏览器访问http://localhost:3000/;这里需要注意 express 4.x 无法以 node app.js 为启动方式,而是用指令 npm start 作为启动;
这里大功告成,终于学会了环境安装,接下去可以好好深入学习了,真开心!!!