NPM Quick Tips — #1 Install Packages

We’re huge fans of Node.js and NPM. Especially, hapi has taken a place in our hearts for the category of Node.js frameworks. And since Node ships with NPM (node package manager) by default, there are multiple tips, tricks and shortcuts which will save you time doing repetitive tasks during your work days (like installing packages).

This article shows you different ways to install Node packages. Additionally, you can add a package directly as a dependency to your project by adding it to your package.json file. We’re using some of our favorite packages within the examples to illustrate NPM’s feature set.

To run the commands used within this article, we leverage NPM in version 2.14.0. You should be save running any 2.x version of NPM. The commands are also available in NPM 3.x.

NPM Quick Tips Series Overview


Before we start installing any package, create a new node project with npm init. This will be the playground if you want to run the commands showed within this post.

Install Package

The most common task with NPM is installing packages. By the time of writing this article, more than 2 billion packages get downloaded monthly. And the number is drastically increasing. That means, there is a high demand on installing packages :)

We’re starting soft. You can install any package with the npm install command. Pass the package name as the third parameter and just wait for the installation to finish.

npm install lodash  

Install Multiple Packages With One Command

NPM offers the ability to install multiple packages within one command. npm install awaits an array of packages separated by space. Passing multiple packages will result in a sequence of package installation processes.

npm install lodash hapi thinky when  

Install Package From Github Repository

This is a kind of secret NPM feature. You can directly install packages from their GitHub repositories. Of course they need to be publicly available so NPM can clone the repository first and install the package from source. Pass the GitHub repository as in place of the package name and NPM will do the work.

npm install  

You don’t necessarily need to pass the complete url path to the GitHub repository. You can shortcut to username/repository and NPM will automatically recognize that you want to install a GitHub repository.

npm install lodash/lodash  

Let’s advance another step when directly install packages from GitHub repositories. If you rely on a specific branch for a package, NPM got you covered. You can specifiy a branch by adding #branchname to the GitHub url.

npm install  

Install Package and Save as Dependency

Packages which were installed with npm install are available locally to your project, but they don’t get added as a dependency within the package.json file. You can install and directly add the newest package version as a dependency to your project by passing the --save flag to the install command.

npm install --save lodash  
npm install --save lodash hapi thinky when  
"dependencies": {
    "hapi": "^9.0.3",
    "lodash": "^3.10.1",
    "thinky": "^2.1.6",
    "when": "^3.7.3"

Combine the install process of multiple packages and add them as dependencies to your project by passing multiple packages to the npm install command including the --save flag.

Install Package and Save as devDependency

NPM allows you to define devDependencies for your project. Usually, these are dependencies to run tests on your code or do further operations like specify the code coverage. You can install and add devDependencies to your project with the help of the --save-dev flag for npm install.

npm install --save-dev mocha  
npm install --save-dev mocha istanbul  
"devDependencies": {
    "istanbul": "^0.3.18",
    "mocha": "^2.2.5"

As well as with usual dependencies, you can install multiple devDependencies by passing several package names and NPM will install and add them as devDependencies automatically to your project.

Install Global Packages

Besides packages which are only installed locally within your project folder, you can install packages globally. Some packages expose command line options like generators or even the actual functionality and make it usable from command line. Add the --global flag when installing a package to install it into the global node_modules folder for your current system user. Global packages won’t get installed into any specific project.

npm install --global mocha  

You can use global packages within any local project and execute specific functionality. Please keep in mind, that the functionality of globally installed packages on your machine isn’t available on the system of your peers. Don’t use those packages within your code.

Even though this is NPM’s basic functionality, we hope you benefit from the tips above and take away a new insight.

Within the next NPM quick tips post, we’ll show you available shortcuts to install packages.

Do you miss any tip? Poke us and let us know in the comments or shout out @futurestud_io.

Additional Resources

Explore the Library

Find interesting tutorials and solutions for your problems.