Introduction to Node

I particularly like this quote from Why The Hell Would I Use Node.js? A Case-by-Case Introduction [] by Tomislav Capan

Before you run any installers, make sure you know what is on your computer. To see the version, simply run:

Of course to create and run a Node app, you need to have Node installed. To install Node, you can run the .

Installing Node and npm is a great article on all the ways to get this set up. Pay attention to Step 4 where there are some pretty solid opinions on how to set things up.

A is made available that illustrates a series of ways to install node.

The article does state a personal opinion against using Homebrew. Brew has worked for me rather well, but this is an opinion you may need formulate on your own.

npm is a NodeJS package manager. As its name would imply, you can use it to install node programs. Also, if you use it in development, it makes it easier to specify and link dependencies.

Depending on your install process you may or may not have NPM installed. To check, simply run:

If you do not have npm installed, run the following:

Note: npm is the package manager for Node, so you can’t use the package manager to install the package manager o_O

  1. $ curl http://npmjs.org/install.sh | sh

Now that you have npm installed, all registered packages are a simple command away. For a basic package install, run:

  1. $ npm install <package>

This install method will install the package in a directory (node_modules/) relative to your project. At times you will need to install libraries globally so that you can access their code from any application that doesn’t necessarily require them as a dependency.

To do this, you need to add the -g flag n the install process:

Note: Depending on how Node is installed on your system, you may not have access to install a global package. To get around this, simply add the sudo command before the npm install method:

  1. $ sudo npm install -g <package>

The most common use case for npm is to maintain a manifest of dependencies for your project. This is maintained with a file.

You can either crate this file yourself, or there are ways to generate this file too. In any directory simply run npm init and the CLI will walk you through a series of questions and out put something like the following:

  1. "name": "toss-this",
  2. "version": "0.0.0",
  3. "description": "",
  4. "main": "index.js",
  5. "scripts": {
  6. "test": "echo \"Error: no test specified\" && exit 1"
  7. },
  8. "license": "ISC"
  9. }
  1. $ npm install <package> --save

Adding Grunt to the project would update the package.json by adding a dependencies object in the json file:

Adding to this, if you wanted to add a dependency that is only for development versus production, you pass in the -dev flag:

  1. $ npm install <package> --save-dev

Adding Gulp as a development dependency we get the following update to the package.json file by adding a devDependencies object:

  1. {
  2. "name": "toss-this",
  3. "description": "",
  4. "scripts": {
  5. "test": "echo \"Error: no test specified\" && exit 1"
  6. },
  7. "author": "",
  8. "license": "ISC",
  9. "dependencies": {
  10. "grunt": "^0.4.5"
  11. },
  12. "devDependencies": {
  13. "gulp": "^3.6.2"
  14. }
  15. }

npm is an amazingly complex utility when it comes to package management. See this npm cheatsheet for more in-depth information.

The package.json has many features. To learn more about how this all works is an amazing tool to learn from.

Unlike other package managers, npm installs these libraries directly into the root of your project. Without extra steps, these libraries will easily get committed to your version control.

For the most part you probably don’t want to do this. Versioning is maintained via the package.json file and you shouldn’t ever really go into the packages themselves and edit the code.

To keep npm libraries out of your version control, add the following to your .gitignore file.

    After executing that command you should see your CLI downloading the internet!