尝试后可以成功在Ubuntu安装node.js的方法
步骤1: 用curl获取源代码
在我们用curl获取源代码之前,我们必须先升级操作系统,然后用curl命令获取NodeSource添加到本地仓库。
[email protected]-15:~#apt-get update
[email protected]-15:~# curl-sL https://deb.nodesource.com/setup | sudo bash -
curl将运行以下任务
## Installing the NodeSource Node.js 0.10 repo...
## Populating apt-get cache...
## Confirming "vivid" is supported...
## Adding the NodeSource signing key to your keyring...
## Creating apt sources list file for the NodeSource Node.js 0.10 repo...
## Running `apt-get update` for you...
Fetched6,411 Bin5s(1,077 B/s)
Readingpackage lists...Done
## Run `apt-get install nodejs` (as root) to install Node.js 0.10 and npm
步骤2: 安装NodeJS和NPM
运行以上命令之后如果输出如上所示,我们可以用apt-get命令来安装NodeJS和NPM包。
[email protected]-15:~# apt-get install nodejs
NodeJS Install
步骤3: 安装一些必备的工具
通过以下命令来安装编译安装一些我们必需的本地插件。
[email protected]-15:~# apt-get install-y build-essential
敲入node命令,结果发现不行,提示是否要运行:apt-get install nodejs-********
按照提示执行,完成!
通过Node.JS Shell来测试
测试Node.JS的步骤与之前使用源代码安装相似,通过以下node命令来确认Node.JS是否完全安装好:
[email protected]-15:~# node
> console.log('Node.js Installed Using Package Manager');
Node.jsInstalledUsingPackageManager
[email protected]-15:~# node
> a =[1,2,3,4,5]
[1,2,3,4,5]
>typeof a
'object'
>5+2
7
>
(^C again to quit)
>
[email protected]-15:~#
使用NodeJS应用进行简单的测试
REPL是一个Node.js的shell,任何有效的JavaScript代码都能在REPL下运行通过。所以让我们看看在Node.JS下的REPL是什么样子吧。
[email protected]-15:~# node
>var repl=require("repl");
undefined
> repl.start("> ");
PressEnterand it will showout put like this:
>{ domain:null,
_events:{},
_maxListeners:10,
useGlobal:false,
ignoreUndefined:false,
eval:[Function],
inputStream:
{ _connecting:false,
_handle:
{ fd:0,
writeQueueSize:0,
owner:[Circular],
onread:[Function: onread],
reading:true},
_readableState:
{ highWaterMark:0,
buffer:[],
length:0,
pipes:null,
...
...
以下是可以在REPL下使用的命令列表
REPL Manual