How to Install Local Packages

In the previous tutorial we walked you through the process of setting up your npm account, we also introduced taught you how you can install npm via the browser and nvm.

In this tutorial we will show you how to install local packages. Local installation of packages is not the only way to install packages, we can also install packages globally. So when should you install globally and when should you install locally:

  • You should install locally when you want to depend on the package from your own module, using something such as Node.js' require. This is default behavior of npm install.
  • When you want to use a package as a command line tool, (like grunt CLI), then you have to install it globally.

Installing a Package

You can download a package with the command below:

npm install <package_name>

This creates the node_modules directory in your current directory (if one doesn't exist yet) and downloads the package to that directory.


If you want to confirm that npm install worked correctly, you should check to see that a node_modules directory exists and that it has a directory for the package(s) that you have installed.


Take for instance that you install a package called express, you can confirm that it worked correctly by checking that a node_modules directory now exists and that the directory has a subdirectory named express.

Microsoft Windows:

npm install express
C:\ dir node_modules

#=> express

macOS, Ubuntu, Debian

> npm install express
>ls node_modules             


Which Version of the Package is Installed?

If a package.json file does not exist in the local directory, the latest version of the package will be installed.

However, if a package.json file exists, npm will install the latest version that satisfies the semver (semantic versioning ) rule that is declared in the package.json.

Using the Installed Package in Your Code

Once you have run npm install <package name> and the package is in the node_modules directory, it can be used in your code. For instance, when you are creating a Node.js module, you can use require to access it.


Create a file and name it index.js, and add the following code:

// index.js
var express = require('express');
console.log("successfully required a package")

when you run the code, you should get the output: successfully required a package.

If you had not installed express properly, you will get the following error message:

    throw err;
Error: Cannot find module 'express'

To fix this error, you should run npm install express in the same directory as your index.js

How to Update Local Packages

You should periodically update the packages that your application depends on. Then if there are code changes made by the original developers, your code will also be improved.

To do this:

  • You should run npm update in the same directory as the package.json file of the application that you want to update.
  • Then run npm outdated. No result is expected.

How to Uninstall Local Packages

If you want to remove a package directly from your node_modules directory, you should use:

npm uninstall <package>

npm uninstall lodash

If you want to remove it from the dependencies in package.json, you have to use the save flag:

npm uninstall --save lodash

Note: if you had installed the package as a "devDependency" (i.e. with --save-dev) then --save will not remove it from package.json. You will need to use --save-dev to uninstall it.


If you want to confirm that npm uninstall worked correctly, you should find the node_modules directory. Ensure that it no longer contains a directory for the package(s) you uninstalled.

This can be done by running:

ls node_modules on Unix systems like "OSX"
dir node_modules on Windows.


Install a package called axios. Then confirm that it ran successfully by listing the contents of the node_modules directory and seeing a directory called axios.

Uninstall axios with npm uninstall. Then confirm that it ran successfully by listing the contents of the node_modules directory and confirming the absence of a directory called axios.

Install Axios

npm install axios
dir node_modules               # use `ls node_modules` for Unix

Uninstall Axios

#=> axios
> npm uninstall axios
> dir node_modules               # use `ls node_modules` for Unix


Previous: How to set up a new npm account & install npm
Next: Working with package