Node.js Development Environment — Ubuntu

Introduction
We will use Ubuntu 12.04 LTS 64bit. If you are using Mac, you can follow the instruction on Installation of VMWare Fusion and Ubuntu. Once your Ubuntu is up, use shortcut Ctrl+Alt+T to open your terminal window.
nodejs-env1

<<Node.js toolkit>> ©2014, Conan Zhang and Vivian S. Zhang. All rights Reserved. The corresponding post written in Chinese can be found at Conan Zhang’s blog. Please contact [email protected] if you are interested to publish it in English.

<<Node.js toolkit>> will introduce you to use Javescript as server side script and use node.js framework to develop website. Nodejs framework is based on the V8 engine which is the fastest Javascript engine. Chrome browser is also based on V8. It is very smooth even when you open 20 to 30 pages simultaneously. Node.js standard web development framework, Express, can help us quickly build web sites. Developing website by node.js is more efficient than doing it by PHP and require less steep learning curve. It is ideal for building small sites and personalized web sites. We want to introduce you a lot of handy tools to reduce your workload and make elegant and beautiful site easily.

Content:

     1. Installed via apt-get nodejs – Failed.

     2. Download the source code from github install – successful.

     3. Establish express project, start your first project.

1. Installation via apt-get nodejs – Failed

Set up the environment

~ sudo apt-get install nodejs
~ sudo apt-get install npm
~ node -v
v0.6.12
~ npm -v 1.1.4

Create working directory

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

Failed installation

Errors Conan got:

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

Prompt indicates that node and npm version are incompatible. It may be caused by failing to update the source by “apt-get”. And you should move on and install compatible node and npm manually.

Errors Vivian got:

vivianzhang@ubuntu:~/workspace/nodejs$ sudo
npm install express -g
npm http GET https://registry.npmjs.org/express
npm ERR! Error: failed to fetch from registry: express
npm ERR! at /usr/share/npm/lib/utils/npm-registry-client/get.js:139:12
npm ERR! at cb (/usr/share/npm/lib/utils/npm-registry-client/request.js:31:9)
npm ERR! at Request._callback (/usr/share/npm/lib/utils/npm-registry-client/request.js:136:18)
npm ERR! at Request.callback (/usr/lib/nodejs/request/main.js:119:22)
npm ERR! at Request. (/usr/lib/nodejs/request/main.js:212:58)
npm ERR! at Request.emit (events.js:88:20)
npm ERR! at ClientRequest. (/usr/lib/nodejs/request/main.js:412:12)
npm ERR! at ClientRequest.emit (events.js:67:17)
npm ERR! at HTTPParser.onIncoming (http.js:1261:11)
npm ERR! at HTTPParser.onHeadersComplete (http.js:102:31)
npm ERR! You may report this log at:
npm ERR!
npm ERR! or use
npm ERR! reportbug --attach /home/vivianzhang/workspace/nodejs/npm-debug.log npm
npm ERR!
npm ERR! System Linux 3.11.0-15-generic
npm ERR! command "node" "/usr/bin/npm" "install" "express" "-g"
npm ERR! cwd /home/vivianzhang/workspace/nodejs
npm ERR! node -v v0.6.12 npm ERR!
npm -v 1.1.4
npm ERR! message failed to fetch from registry: express
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/vivianzhang/workspace/nodejs/npm-debug.log
npm not ok
vivianzhang@ubuntu:~/workspace/nodejs$

The solution for “Error: failed to fetch from registry: express” is run the following in your terminal window:

vivianzhang@ubuntu:~/workspace/nodejs$ npm config set registry http://registry.npmjs.org/
vivianzhang@ubuntu:~/workspace/nodejs$ sudo npm install express -gnpm http GET http://registry.npmjs.org/express
npm http 200 http://registry.npmjs.org/express
npm http GET http://registry.npmjs.org/express/-/express-3.4.4.tgz
npm http 200 http://registry.npmjs.org/express/-/express-3.4.4.tgz
npm http GET http://registry.npmjs.org/connect/2.11.0
npm http GET http://registry.npmjs.org/commander/1.3.2
npm http GET http://registry.npmjs.org/range-parser/0.0.4
npm http GET http://registry.npmjs.org/mkdirp/0.3.5
npm http GET http://registry.npmjs.org/cookie/0.1.0
npm http GET http://registry.npmjs.org/buffer-crc32/0.2.1
npm http GET http://registry.npmjs.org/fresh/0.2.0
npm http GET http://registry.npmjs.org/cookie-signature/1.0.1
npm http GET http://registry.npmjs.org/methods/0.1.0
npm http GET http://registry.npmjs.org/send/0.1.4
npm http GET http://registry.npmjs.org/debug
npm http 200 http://registry.npmjs.org/cookie/0.1.0
npm http GET http://registry.npmjs.org/cookie/-/cookie-0.1.0.tgz
npm http 200 http://registry.npmjs.org/buffer-crc32/0.2.1
npm http GET http://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.1.tgz
npm http 200 http://registry.npmjs.org/commander/1.3.2
npm http 200 http://registry.npmjs.org/connect/2.11.0
npm http 200 http://registry.npmjs.org/mkdirp/0.3.5
npm http GET http://registry.npmjs.org/commander/-/commander-1.3.2.tgz
npm http GET http://registry.npmjs.org/connect/-/connect-2.11.0.tgz
npm http GET http://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz
npm http 200 http://registry.npmjs.org/range-parser/0.0.4
npm http GET http://registry.npmjs.org/range-parser/-/range-parser-0.0.4.tgz
npm http 200 http://registry.npmjs.org/fresh/0.2.0
npm http GET http://registry.npmjs.org/fresh/-/fresh-0.2.0.tgz
npm http 200 http://registry.npmjs.org/cookie/-/cookie-0.1.0.tgz
npm http 200 http://registry.npmjs.org/send/0.1.4
npm http 200 http://registry.npmjs.org/methods/0.1.0
npm http 200 http://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.1.tgz
npm http 200 http://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz
npm http GET http://registry.npmjs.org/send/-/send-0.1.4.tgz
npm http GET http://registry.npmjs.org/methods/-/methods-0.1.0.tgz
npm http 200 http://registry.npmjs.org/debug
npm http 200 http://registry.npmjs.org/range-parser/-/range-parser-0.0.4.tgz
npm http GET http://registry.npmjs.org/debug/-/debug-0.7.4.tgz
npm http 200 http://registry.npmjs.org/commander/-/commander-1.3.2.tgz
npm http 200 http://registry.npmjs.org/connect/-/connect-2.11.0.tgz
npm http 200 http://registry.npmjs.org/send/-/send-0.1.4.tgz
npm http 200 http://registry.npmjs.org/fresh/-/fresh-0.2.0.tgz
npm http 200 http://registry.npmjs.org/cookie-signature/1.0.1
npm http 200 http://registry.npmjs.org/debug/-/debug-0.7.4.tgz
npm http 200 http://registry.npmjs.org/methods/-/methods-0.1.0.tgz
npm http GET http://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.1.tgz
npm http 200 http://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.1.tgz
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.11.0-15-generic
npm ERR! command “node” “/usr/bin/npm” “install” “express” “-g”
npm ERR! cwd /home/vivianzhang/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 http://registry.npmjs.org/mime
npm http GET http://registry.npmjs.org/keypress
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/vivianzhang/workspace/nodejs/npm-debug.log
npm not ok

Prompt indicates that node and npm version are incompatible. As of this moment, both Conan and Vivian reached the same point of progress.

2. Download the source code from github install – successful

To fix errors Conan and Vivian got, we would find compatible version, and install them manually.

Uninstall the node and npm we got from last step

~ sudo apt-get autoremove npm
~ sudo apt-get autoremove nodejs

Find the node official repo: https://github.com/joyent/node

Install git for your ubuntu

~ sudo apt-get install git

install nodejs from github

~ 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.

Go inside of node directory

~ cd node
~ pwd
/home/vivianzhang/workspace/nodejs/node

switch the new repo branch

~ 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'

Install node

./configure
make
sudo make install

after installation is finished, check version of node

~ node -v
-bash: /usr/bin/node: No such file or directory

We got prompt which states ” no node”, let us check the directory node was installed:

Conan got:

~ whereis node
node: /usr/local/bin/node

Vivian got:

~ whereis node
node:

If you got errors like Vivian did,  do the following:

Based on stackoverflow post, we can fix it by running:

sudo apt-get purge nodejs npm

Then install from Chris Lea’s repo:

sudo apt-get update
sudo apt-get install -y python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

Run the command again, you should get the same output Conan got:

~ whereis node
node: /usr/local/bin/node

As of this moment, both Conan and Vivian reached the same point of progress. Then do the following:

Add symlink for node

~ sudo ln -s /usr/local/bin/node /usr/bin/node
~ sudo ln -s /usr/local/bin/npm /usr/bin/npm

Then we check version of node and npm again

~ node -v
v0.11.2
~ npm -v
1.2.21

We install 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], [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])vivianzhang@ubuntu:~/workspace/nodejs/node$

The installation is successful now.

3. Establish express project , start your first project

~ 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

Install the dependent packages

~ 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], [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])
vivianzhang@ubuntu:~/workspace/nodejs/node/nodejs-demo$

Start the app

vivianzhang@ubuntu:~/workspace/nodejs/node/nodejs-demo$ node app.js
Express server listening on port 3000

Test whether we can call curl by opening another terminal window by ctrl+alt+T

vivianzhang@ubuntu:~$ curl localhost:3000
<!DOCTYPE html>
<html>
<head>
<title>Express</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>Express</h1>
<Welcome to Express</p>
<body>
<html>

From previous terminal window, you will see Node.js server log

vivianzhang@ubuntu:~/workspace/nodejs/node/nodejs-demo$ node app.js
Express server listening on port 3000
GET / 200 14ms - 206b

Hooray, we set up node.js environment in Ubuntu. Enjoy developing!