Premium lesson

Installing & Using npm Packages

npm·4 min read·Jan 1, 2025

In npm, packages can be downloaded and installed in two different ways: locally and globally.

A local package is a package installed within the top-level directory of a project and cannot be accessed by other projects on your machine.

It allows each project to manage their own packages independently, without interfering with others.

A global package, on the other hand, is a package installed within the global npm installation directory and can be accessed by any project on your machine.

It may also include command-line tools that can be run from any directory.

Initializing a project

Before being able to install packages locally, you will have to initialize the project you want to install the packages into, which essentially means creating a package.json file.

The package.json file

The package.json file is a special type of file called a dependency declaration manifest.

It includes the list of packages your project depends on (its dependencies) and their versions, as well as additional information about the project itself, such as its name, description, author, repository URL, and so on.

When installing a local package, npm automatically updates the package.json file to include it in the list of dependencies, so that they can be easily reinstalled in the future; since the dependencies of a project are not supposed to be uploaded on a version control system such as Git.

Creating the package.json file

To initializing a new or existing project, you can:

  1. Enter your project's directory

    $ cd ~/projects/api
icon light bulb key

Unlock the Build CLI Apps in JavaScript & Node.js module

Learn how to build, integrate, and safeguard scalable CLI tools, scripts, and apps with JavaScript, Node.js, npm, and Git.

You get immediate access to:

  • 45 focused lessons across JavaScript, Node.js, npm, and Git
  • 17 real-world projects with commented solutions
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module