[Tutorial] Install Node.js, NPM and Express in Ubuntu 12.04

By Budyks    NodeJs

Node.js is a platform built on  for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.   - http://nodejs.org/   
If you dont want to install it via Terminal , you check itu here http://nodejs.org/download/ 

Install Node.js

you can Install it using Chris Lea launchpad repo, and type these commands one by one : 
$ sudo apt-get install python-software-properties$ sudo apt-add-repository ppa:chris-lea/node.js$ sudo apt-get update$ sudo apt-get install nodejs
Test your nodes to make sure that its installed successfully by just typing 'node' :
> 3 + 3

or create a file somewhere in your directory (for ex : app.js )

and type this :

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '');
console.log('Server running at');

save the file, then execute it via terminal like this:

node app.js 

After that you can check your installed node Version with this command :
node -v
To make your life easier with Nodejs, you can install NPM ( Nodejs Package Modules ). its a module manager built for Nodejs.

Install NPM

type this command in your terminal
sudo apt-get install npm
once installed, you can now easily manage modules or packages you wish to include to your Nodejs Project
Below this line is a bit outdated, I strongly suggest you hit this : .
Create a folder (eg : project) somewhere in your machine, go inside it and create a json file named package.json and server.js. copy this code and paste into package.json
  "name": "project",
  "description": "My Express project",
  "version": "0.0.1",
  "private": true,
  "main": "server.js",
  "dependencies": {
    "body-parser": "1.9.0",
    "ejs": "^1.0.0",
    "express": "4.9.5", 
now you hit npm install 
ubuntu@machine:~/project$ npm install
CLick here for writing your first CRUD Project*BELOW THIS LINE IS A BIT OUTDATED

Install Express

Express is a Nodejs Framework. And again this cool stuff could make your node-life is easier. 
type this to install it 
npm install express
to make an easy Application or project with express you can install it again (doesnt matter) with this : 
npm install -g express
and now , to create your nodes App you can just type
express myapp
Express will generate your default ( you modify it your way later) modules for you like this
   create : myapp
   create : myapp/package.json
   create : myapp/app.js
   create : myapp/public
   create : myapp/public/javascripts
   create : myapp/public/images
   create : myapp/public/stylesheets
   create : myapp/public/stylesheets/style.css
   create : myapp/routes
   create : myapp/routes/index.js
   create : myapp/routes/user.js
   create : myapp/views
   create : myapp/views/layout.jade
   create : myapp/views/index.jade
Run you apps with  
cd myapp

node app.js
And your Nodes is ready !!!.
I have written some example of nodejs apps here > Simple CRUD Node.js & MySQL


    Follow Us