尝试后可以成功在Ubuntu安装node.js的方法

来源:互联网 发布:淘宝机票平台出租 编辑:程序博客网 时间:2024/05/20 20:04

 

步骤1: 用curl获取源代码

在我们用curl获取源代码之前,我们必须先升级操作系统,然后用curl命令获取NodeSource添加到本地仓库。

  1. root@ubuntu-15:~#apt-get update
  2. root@ubuntu-15:~# curl-sL https://deb.nodesource.com/setup | sudo bash -

curl将运行以下任务

  1. ## Installing the NodeSource Node.js 0.10 repo...
  2. ## Populating apt-get cache...
  3. ## Confirming "vivid" is supported...
  4. ## Adding the NodeSource signing key to your keyring...
  5. ## Creating apt sources list file for the NodeSource Node.js 0.10 repo...
  6. ## Running `apt-get update` for you...
  7. Fetched6,411 Bin5s(1,077 B/s)
  8. Readingpackage lists...Done
  9. ## Run `apt-get install nodejs` (as root) to install Node.js 0.10 and npm

 

步骤2: 安装NodeJS和NPM

运行以上命令之后如果输出如上所示,我们可以用apt-get命令来安装NodeJS和NPM包。

  1. root@ubuntu-15:~# apt-get install nodejs

NodeJS Install

NodeJS Install

 

步骤3: 安装一些必备的工具

通过以下命令来安装编译安装一些我们必需的本地插件。

  1. root@ubuntu-15:~# apt-get install-y build-essential


敲入node命令,结果发现不行,提示是否要运行:apt-get install nodejs-********

按照提示执行,完成!



通过Node.JS Shell来测试

测试Node.JS的步骤与之前使用源代码安装相似,通过以下node命令来确认Node.JS是否完全安装好:

  1. root@ubuntu-15:~# node
  2. > console.log('Node.js Installed Using Package Manager');
  3. Node.jsInstalledUsingPackageManager

  1. root@ubuntu-15:~# node
  2. > a =[1,2,3,4,5]
  3. [1,2,3,4,5]
  4. >typeof a
  5. 'object'
  6. >5+2
  7. 7
  8. >
  9. (^C again to quit)
  10. >
  11. root@ubuntu-15:~#

 

使用NodeJS应用进行简单的测试

REPL是一个Node.js的shell,任何有效的JavaScript代码都能在REPL下运行通过。所以让我们看看在Node.JS下的REPL是什么样子吧。

  1. root@ubuntu-15:~# node
  2. >var repl=require("repl");
  3. undefined
  4. > repl.start("> ");
  5. PressEnterand it will showout put likethis:
  6. >{ domain:null,
  7. _events:{},
  8. _maxListeners:10,
  9. useGlobal:false,
  10. ignoreUndefined:false,
  11. eval:[Function],
  12. inputStream:
  13. { _connecting:false,
  14. _handle:
  15. { fd:0,
  16. writeQueueSize:0,
  17. owner:[Circular],
  18. onread:[Function: onread],
  19. reading:true},
  20. _readableState:
  21. { highWaterMark:0,
  22. buffer:[],
  23. length:0,
  24. pipes:null,
  25. ...
  26. ...

以下是可以在REPL下使用的命令列表

REPL Manual

REPL Manual

原创粉丝点击