First Time Node - #2 Packages

Node comes bundled with npm for managing dependencies. You can import as many libraries from npm into your application as you want. There is a package for almost anything you could imagine in npm, and it includes well-maintained packages from large businesses all the way down to random side-projects from individuals.

This is both a strength and a weakness. The strength is that you can get work done fast by importing libraries rather than building certain functionality yourself, but the downside is that you can become too dependent on third-party libraries. It's generally a good idea to try and keep the number of dependencies in your application to a minimum. You might still end up with a long list of dependencies, but we all do our best.

Step #1: Import a package

If you read my last post, #1: Getting Started, then you already have created a little Node project. So, feel free to use that or create a new one.

The package we are installing today is called colors. It is a package for adding colors to your command-line output. So, from the project root, enter npm install colors --save into your terminal. This will install the colors package. You don't have to add the --save at the end, but you must save it in order to have the dependency recorded in your package.json file.

Now, open up your package.json file. It should look something like this:

{
  "name": "test-proj-something",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "",
  "license": "Apache-2.0",
  "dependencies": {
    "colors": "^1.1.2"
  }
}

Notice how the colors package is now listed there. This is what you want, because now your application is portable. You can take your project files (in this case index.js and package.json) and put them on a server or another computer, or into a GIT repository and you do not have to carry your dependencies (the actual files of your dependencies) along with you. You just plop your project files down somewhere and run npm install in the project directory, then npm will look through those dependencies and install them! So, always use the --save flag when adding dependencies.

Now, take a moment to look at the files and folders in your project directory. You will notice a new node_modules directory. This is where your dependencies are stored. If you open up the folder, you will see the colors package.

Step #2: Require the package

Open up your index.js file. What we want to do now is require or import the colors package. At the top of your index.js file, write:

var colors = require('colors');

require() is a built-in global method in Node. You pass a string into the method with the name or path of the package you are requiring. In this case, you do not need to worry about a path to the colors package. Since you downloaded the dependency from npm, you can just enter the name of the package. If you ever do not know the name to require, just check your package.json file and you will see the name recorded there, or check the node_modules folder and the name of the package's folder is the name which you will require.

Step #3: Use the package!

We required the colors package and we put the contents of that package into a variable named colors, so now let's use it!

The colors package gives you an object with a number of useful methods for coloring command-line output. Let's log a green sentence. Enter the following into your index.js file.

var colors = require('colors');

console.log(colors.green('Green is my favorite color!'));

Save your file and enter npm start to see your script run. It will log 'Green is my favorite color!' to your console in green.

Step #4: Conclusion

I considered going into how to create your own local packages, but I want to wait until we have set up our build process to use the new ES2015 syntax of JavaScript. It is much more clear and concise than the previous way of using module.exports (which we did not talk about here) and require().

But, this is enough to get you downloading and using packages/libraries/dependencies from npm. Node is an exciting place to be and if you are ever looking for a package you need, check out npmjs.org and search for what you are looking for.

In the next post, I will show you how to set up a decent Node development environment using the free and open-source Atom editor.


This is part two of an ongoing series: