Outdated: please disregard this article as it is out of date. I install node in my local filespace on Ubuntu now as its so much easier to manage. Basicaly I download the Linux binaries and put them in ~/apps/nodejs/current, then add ~/apps/nodejs/current/bin to my path using my shell profile (~/.profile). This makes using npm -g really easy and does not require the sudo command.
Whilst there is a nodejs package in Ubuntu, it is version 0.6.9 and therefore quite a way behind the current version on the nodejs website. So lets do a manual install with the latest version, 0.10.1.
I since found an alternative approach using ppa’s but haven’t tried it out.
Download the install archive file and extract it. I chose to do this in a folder called apps in my home folder. Alternatively you could install it in
mkdir ~/apps/nodejs tar zvxf node-v0.10.1.tar.gz
As we are doing a manual install, we need to build nodejs to get the actual executable files. This requires a C compiler on your laptop which is not installed by default. So either use the Ubuntu software center to install the package
g++ or use the command line
sudo apt-get install g++
To compile nodejs, first we run configuration to check all the neccessary external libraries are there and then we make node:
Add the following to your environment in your
~/.bashrc file (or
.zshrc file if you are running zshell). I moved the node executable file created by the compile process into a folder called bin, so I knew which was the right file to run. Then I added that folder to the path.
export NODEJS_HOME=/home/jr0cket/apps/nodejs/bin export PATH=$PATH:$NODEJS_HOME
I am using an environment variable called NODEJS_HOME as a convienience. You can just add the whole path in one line.
The node package manager is a great way to get additional libraries into your node projects. It does not come with node itself, so you have to install it seperately. Npm also needs node installed first.
On the node package manager website, the install process is defined as the following command:
curl https://npmjs.org/install.sh | sh
In my manual install (not using Ubuntu packages) then node and npm are created in different folders. So I put the npm executable file in the same bin folder I created previously for node, which I had already added that to the executable path.
Once npm is installed you can search for and install packages. If you the
-g option for npm install then the modules will be installed globally, otherwise any modules will be local to your project in an npm-modules folder.
Search for modules:
npm search mongodb native
Install modules locally or globally:
npm install mongodb npm install -g mongodb
You can run an interactive session for nodejs (the node REPL) using the command:
So lets create a simple “Hello World” app for nodejs in a file called web.js
var express = require('express');
Running this with
node web.js we get “Hello World” as the output.
nodejs is one of the languages supported on Heroku (a cloud service that gives developers a sane way to deploy and scale their apps). Deploying this nodejs app on Heroku is therefore really trivial.
Heroku can usually work out what to do with many projects, based on the language and framework used. However, just to be specific lets create a
Procfile to tell node which is our entry point to our application. In this case we want node to start with the file
web: node web.js
Lets version the project with git
git init git add . git commit -m "Initial project setup"
Heroku adds a new remote to our git project called heroku, so we can push our code to our app.
Now that our project is ready to deploy, lets push all the code to the heroku application you created using git push, specifying the branch you are pushing (usually
git push heroku master
Now open the node website in a browser using the URL given after the upload of your code via git push, or just the command
There is a nice article about nodejs on heroku with examples of wiring node up to various data sources too.
- YouTube videos
- Node Package Manager
- Nodejs for beginners
- HowToNode - community supported blog to teach fundamental concepts for writing effective code along with various other tips.
- Node-inspector - Web Inspector based nodeJS debugger
- Nodejitsu - a growing collection of node.js how-to articles from the community, range from basic to advanced.
- Superhero.js - a collection of articles, presentations and videos
- Nodejs google group**
- Suggestions of learning materials on Stack Exchange
This work is licensed under a Creative Commons Attribution 4.0 ShareAlike License, including custom images & stylesheets. Permissions beyond the scope of this license may be available at @jr0cket