Node JS setup
Contents
Core installation
- Node || NodeJS ==> NodeJS server runtime
- NPM ==> NodeJS libraries manager
Automatic installation
sudo apt-get install nodejs nodejs-legacy npm
Check versions
nodejs --version
npm --version
Manual installation
The package version is usually quite "late" compare to the current one. I recommand you to use the manual installation. It's also easier to manage version evolutions ; as 'apt-get upgrade' might mess up your current settings.
Download NodeJS
Go to http://nodejs.org/download/ and get the latest binary version for your system.
Extract it into a dedicated folder and create symlinks
tar xzvf node-v0.10.33-linux-x64.tar.gz
rm node-v0.10.33-linux-x64.tar.gz
mv node-v0.10.33-linux-x64/ nodeJs-v0.10.33
ln -s /home/guillaume/Dev/nodeJs-v0.10.33 /home/guillaume/Dev/nodeJs
sudo ln -s /home/guillaume/Dev/nodeJs/bin/node /usr/bin/node
sudo ln -s /home/guillaume/Dev/nodeJs/bin/npm /usr/bin/npm
Check versions
node --version
npm --version
Libraries
Bower
Official website: http://bower.io/
Bower is used to managed project's dependencies ~ like Maven on the Java world.
Installation
sudo npm install -g bower
Usage
Go to the root of your project and simply execute:
bower install
Project configuration
You need to create a bower.json
at the root of your project. Set your projects settings & dependencies in that file.
You can find the list of available packages: http://bower.io/search/
Example - source code from official AngularJS tutorial:
{
"name": "angular-phonecat",
"description": "A starter project for AngularJS",
"version": "0.0.0",
"homepage": "https://github.com/angular/angular-phonecat",
"license": "MIT",
"private": true,
"dependencies": {
"angular": "1.3.x",
"angular-mocks": "1.3.x",
"jquery": "1.10.2",
"bootstrap": "~3.1.1",
"angular-route": "1.3.x",
"angular-resource": "1.3.x",
"angular-animate": "1.3.x"
}
}
HTTP server
Official website: https://github.com/nodeapps/http-server
http-server is a lightweight server.
Installation
npm install -g http-server
Usage
Go to the root of your project, then run:
http-server -p 8000 -c-1
Now you can go to http://localhost:8000 ; you should see your project.
Key options:
- -p Port to use (defaults to 8080)
- -a Address to use (defaults to 0.0.0.0)
- -c Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds. To disable caching, use -c-1.
Jasmine
Jasmine is a test framework. That's the one used by the official AngularJS team && some related libraries. You should get familiar with that framework if you plan to use AngularJS.
Official website: http://jasmine.github.io/
Karma
Karma is a tool to run your tests inside some real browsers and devices (phones, tab, ...).
Installation
npm install -g karma
By default Karma register itself into:
- Your current project as a "node-modules" directory
- Common NodeJs modules into /usr/lib/node_modules/
NPM does not create a standard symlink in /usr/bin/! That's because you can use different version of Karma, depending on your projects.
You can create a link manually:
sudo ln -s /usr/lib/node_modules/karma/bin/karma /usr/bin/karma
Project configuration
karma init karma.conf.js
Answer the following questions:
- Which testing framework do you want to use ? > jasmine
- Do you want to use Require.js ? > no
- Do you want to capture a browser automatically ? > Chrome > Firefox
- What is the location of your source and test files ? > test/unit/**/*.js
- Should any of the files included by the previous patterns be excluded ? >
- Do you want Karma to watch all the files and run the tests on change ? > yes
Config file generated at "[...]/karma.conf.js".
You need to adjust the configuration to set the basePath and other parameters.
vim karma.conf.js
Since your configuration is in myProjet/test/ you should set a relative path:
basePath : '../',
Usage
Go to your project's test folder and run:
karma start karma.conf.js
Hints
Karma provides advanced feature for AngularJS. You can use the following methods:
dump(myVariable)