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
$ 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:
$ 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:
$ 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:
"name": "toss-this",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC"
}
$ 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:
$ 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:
{
"name": "toss-this",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"grunt": "^0.4.5"
},
"devDependencies": {
"gulp": "^3.6.2"
}
}
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!