分类
互联网

安装Node.js最新版以及NPM(非Win版)

参考: https://ariejan.net/2011/10/24/installing-node-js-and-npm-on-ubuntu-debian/

  1. 升级系统并安装适当的编译开发环境
    sudo apt-get update
    sudo apt-get install git-core curl build-essential openssl libssl-dev
    
  2. 安装Node.js
    首先克隆Node.js

    git clone https://github.com/joyent/node.git

    然后切换到node目录, 并选择最新版本

    git tag # Gives you a list of released versions
    git checkout v0.9.12
    

    现在, 我们编译并安装Node.js

    ./configure
    make
    sudo make install
    

    你可以使用node -v查看安装是否成功.

  3. 安装包管理器npm
     curl https://www.npmjs.org/install.sh | sudo sh
    

    你可以使用npm -v查看安装是否成功.


至此, Node.js+npm已完全安装成功.

下面, 我们来做一个node.js主页给出的测试.

mkdir mytest
cd mytest
vim example.js
#将下面代码复制到example.js
#---代码分割---
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
#---代码分割---
#然后在当前目录的终端运行
node example.js
#显示:Server running at http://127.0.0.1:1337/, 表示正常运行.

自行测试下一个例子.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据