这篇文章主要介绍了Ubuntu中搭建Nodejs开发环境过程,比较郁闷的是apt-get安装失败了,如果有遇到一样问题的朋友,可以参考一下本文

系统环境:

Ubuntu 12.04 LTS 64bit

一、 通过apt-get安装nodejs – 失败(可以下面的看源码安装方法):


~ sudo apt-get install nodejs
~ sudo apt-get install npm

~ node -v
v0.6.12

~ npm -v
1.1.4


创建工作目录

~ mkdir workspace
~ mkdir workspace/nodejs
~ cd workspace/nodejs
~ pwd
/home/conan/workspace/nodejs

安装失败

sudo npm install express -g
npm http GET https://registry.npmjs.org/express
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/connect/2.7.11
npm http GET https://registry.npmjs.org/commander/0.6.1
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/mkdirp/0.3.4
npm http GET https://registry.npmjs.org/cookie/0.1.0
npm http GET https://registry.npmjs.org/buffer-crc32/0.2.1
npm http GET https://registry.npmjs.org/fresh/0.1.0
npm http GET https://registry.npmjs.org/methods/0.0.1
npm http GET https://registry.npmjs.org/send/0.1.0
npm http GET https://registry.npmjs.org/cookie-signature/1.0.1
npm http GET https://registry.npmjs.org/debug
npm http 304 https://registry.npmjs.org/commander/0.6.1
npm http 304 https://registry.npmjs.org/connect/2.7.11
npm http 304 https://registry.npmjs.org/range-parser/0.0.4
npm http 304 https://registry.npmjs.org/mkdirp/0.3.4
npm http 304 https://registry.npmjs.org/cookie/0.1.0
npm http 304 https://registry.npmjs.org/buffer-crc32/0.2.1
npm http 304 https://registry.npmjs.org/fresh/0.1.0
npm http 304 https://registry.npmjs.org/methods/0.0.1
npm http 304 https://registry.npmjs.org/send/0.1.0
npm http 304 https://registry.npmjs.org/cookie-signature/1.0.1
npm http 304 https://registry.npmjs.org/debug
npm ERR! error installing [email protected]
npm ERR! error rolling back [email protected] Error: UNKNOWN, unknown error '/usr/local/lib/node_modules/express'

npm ERR! Unsupported
npm ERR! Not compatible with your version of node/npm: [email protected]
npm ERR! Required: {"node":">= 0.8.0"}
npm ERR! Actual: {"npm":"1.1.4","node":"0.6.12"}
npm ERR!
npm ERR! System Linux 3.5.0-23-generic
npm ERR! command "node" "/usr/bin/npm" "install" "express" "-g"
npm ERR! cwd /home/conan/workspace/nodejs
npm ERR! node -v v0.6.12
npm ERR! npm -v 1.1.4
npm ERR! code ENOTSUP
npm ERR! message Unsupported
npm ERR! errno {}
npm http GET https://registry.npmjs.org/mime/1.2.6
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/conan/workspace/nodejs/npm-debug.log
npm not ok


系统提示, node和npm版本不兼容。可能是终于apt-get源没有更新造成的问题。

二、从github下载源代码安装 – 成功

下面要找到兼容的版本,手动安装。
先卸载刚刚装的node和npm


~ sudo apt-get autoremove npm
~ sudo apt-get autoremove nodejs
找到nodejs的官方发布下载:https://github.com/joyent/node


在ubuntu中,先安装git

~ sudo apt-get install git

然后,从github下载nodejs源代码
~ git clone git://github.com/joyent/node.git
Cloning into 'node'...
remote: Counting objects: 100200, done.
remote: Compressing objects: 100% (28074/28074), done.
remote: Total 100200 (delta 78807), reused 90936 (delta 70473)
Receiving objects: 100% (100200/100200), 61.81 MiB | 698 KiB/s, done.
Resolving deltas: 100% (78807/78807), done.

进入node目录
~ cd node
~ pwd
/home/conan/workspace/nodejs/node

切换最新的release的版本v0.11.2-release
~ git checkout v0.11.2-release
Branch v0.11.2-release set up to track remote branch v0.11.2-release from origin.
Switched to a new branch 'v0.11.2-release'

进行安装
./configure
make
sudo make install

安装完成,查看node版本
~ node -v
-bash: /usr/bin/node: No such file or directory

提示错误,没有找到node,查一下node安装位置
~ whereis node
node: /usr/local/bin/node

增加软链接:node和npm到/usr/bin
~ sudo ln -s /usr/local/bin/node /usr/bin/node
~ sudo ln -s /usr/local/bin/npm /usr/bin/npm

我们再查看node和npm版本
~ node -v
v0.11.2
~ npm -v
1.2.21

下面安装express

~ sudo npm install express -g
[email protected] /usr/local/lib/node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected])

安装成功。

三、建立express工程,启动第一个项目


~ express -e nodejs-demo
create : nodejs-demo
create : nodejs-demo/package.json
create : nodejs-demo/app.js
create : nodejs-demo/public
create : nodejs-demo/public/javascripts
create : nodejs-demo/public/images
create : nodejs-demo/public/stylesheets
create : nodejs-demo/public/stylesheets/style.css
create : nodejs-demo/routes
create : nodejs-demo/routes/index.js
create : nodejs-demo/routes/user.js
create : nodejs-demo/views
create : nodejs-demo/views/index.ejs
install dependencies:
$ cd nodejs-demo && npm install
run the app:
$ node app

安装依赖包

~ cd nodejs-demo
~ sudo npm install
[email protected] node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected]5, [email protected], [email protected])

启动程序

~ node app.js
Express server listening on port 3000

测试是否启动成功curl


~ sudo apt-get install curl
~ curl localhost:3000

<!DOCTYPE html>
<html>
<head>
<title>Express</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Express</h1>
<p>Welcome to Express</p>
</body>
</html>

nodejs的服务器日志:

GET / 200 6ms - 206b

好了,我们已经成功的在ubuntu中,准备好了nodejs的开发环境。下面就可以开发的了。

最新资讯
人民日报:“菜篮子”供应足 农产品线上购销活跃

人民日报:“菜篮子”供

消费者居家隔离,线下交易大幅减少,线上交易成为消费者的
疫情带动“宅”经济 彩电行业迭代升级提速

疫情带动“宅”经济

对于彩电行业来说,新型冠状病毒感染的肺炎疫情除了对其
蔚来汽车资金“迷局”:遭高瓴清仓 传吉利入股

蔚来汽车资金“迷局”

作为造车新势力的头部玩家,蔚来汽车在资本市场的频频动
特斯拉“单挑”电池巨头:强势买方的超级野心

特斯拉“单挑”电池巨

上海工厂投产后,特斯拉的动力电池供应链终于开始真正地
疫情“压裂”蛋壳  高速扩张带来的隐与忧

疫情“压裂”蛋壳 高

连日来,长租公寓蛋壳、自如在多个城市遭房东、租客双重
采用无钴电池即磷酸铁锂电池?特斯拉说不一定

采用无钴电池即磷酸铁

2月21日,特斯拉表示:”请留意四月特斯拉的电池发布会,无
最新文章
Vue实现图片与文字混输效果

Vue实现图片与文字混

用多了 JQuery ,习惯了使用JQuery的API操作 DOM ,几乎忘
Vue实现点击当前元素以外的地方隐藏当前元素(实现思路)

Vue实现点击当前元素

这篇文章主要介绍了Vue实现点击当前元素以外的地方隐
Vue实现验证码功能

Vue实现验证码功能

这篇文章主要为大家详细介绍了Vue实现验证码功能,文中
JS实现的雪花飘落特效示例

JS实现的雪花飘落特效

这篇文章主要介绍了JS实现的雪花飘落特效,结合实例形
微信小程序实现图片压缩

微信小程序实现图片压

这篇文章主要为大家详细介绍了微信小程序实现图片压缩
JavaScript实现京东放大镜效果

JavaScript实现京东放

这篇文章主要为大家详细介绍了JavaScript实现京东放大