What the Heck is NPM?

By: Brad van der Laan, C.Tech.Brad van der Laan, C.Tech.

Hi Brad,
I started looking into doing web development and have been hearing about NPM; what the heck is that?

ThinkingPig

NPM stands for Node Package Manager and as the name implies its a package manager for JavaScript. NPM is used to share reusable packages of code so you can focus on building your application and not re-inventing the wheel.

The JavaScript community of developers have been creating libraries for all sorts of functionality. Its likely that if your application needs to interface with a public API or support a common feature such as e-mail support there will already be a library you can import saving you the trouble of developing it your self.

NPM is a service not unlike Ruby gems or .Net’s NuGet. There is a website you can go to called npmjs.com that you can use to search for different packages that you might need. Just like Ruby Gems or NuGet, NPM comes with tools that allow you to easily download and install the packages either globally on your system or on a project per project bases.

The NPM tools are installed into your environment automatically when you install Node.js. To use it to install a package you simply have to type the following from a command prompt:

> npm install package_name

The above will install the package local to the project your terminal is currently in. If you are installing a development tool which you’ll use only on your development machine, such as a code generator, you can install it globally so its always there no matter which project your working on. To tell NPM to install a package globally you simply need to use the ‐‐global flag (or simply -g for short).

> npm install -g package_name

The nice thing about NPM is that you, like with Ruby gems or NuGet, can specify which packages your application will depend on that way other developers, or the build machine, will know what they need to install when they clone the repository. This saves you having to check in the packages into your repository; you just need to check in a json configuration file called package.json which lists what the dependencies are.

{
    “name”: “CoolApp”,
    “version”: “0.1.0”,
    “description”: “My cool app”,
    “main”: “app.js”,
    “author”: {
        “name”: “Brad van der Laan”,
        “email”: “”
    },
    “dependencies”: {
        “express”: “~4.13.4″,
        “debug”: “~2.2.0″
    }
}

The above package.json file indicates that the CoolApp project depends on the express, and debug modules.

When you clone a new repository you need to tell NPM to read the configuration file and download all the required dependencies. To do that simply execute the following command:

> npm install

Notice that you do not specify a package name; if no package name is provided NPM will look for a package.json file in the current directory and if it finds one will install any missing dependencies listed in the file.

When you are adding a new dependence and you know this will be one used in production and needs to be installed by NPM on all the development, staging, and production servers you need to remember to update the configuration file. Because who wants to have to remember that or have to remember the json syntax for specifying dependence NPM has another flag you can use which will tell NPM to go and update the json configuration file for you. To add a new dependency and have NPM update the configuration file use the ‐‐save flag (or -s for short).

> npm install -s package_name

Sometimes however the package you are importing into your project might only be needed when you are developing such as a debugging package. Although it won’t hurt to install it on the production servers we can omit them to reduce the projects foot print and speed up deployments. The package.json file can list dependencies for production and development environments separately. This way NPM will know which dependencies it should install depending on the environment your deploying to. You can tell NPM if the package you are adding is only for development environments by using the ‐‐save-dev flag instead of the ‐‐save flag.

> npm install ‐‐save-dev package_name

I recommend checking out the documentation for NPM at their website npmjs.com for a more in-depth reference to the NPM command line interface.

So that is what the heck NPM, or Node Package Manager is; a catalog of re-usable libraries which you can easily download via simple command line tools.

I hope that helps, if you have any questions feel free to leave them below and I’ll reply as time permits.

Until next time think imaginatively and design creatively

Join the Conversation