LinuxCommandLibrary

npm-cache

Manages the npm package cache

TLDR

Add a specific package to the cache

$ npm cache add [package_name]
copy

Remove a specific package from the cache
$ npm cache remove [package_name]
copy

Clear a specific cached item by key
$ npm cache clean [key]
copy

Clear the entire npm cache
$ npm cache clean --force
copy

List the contents of the npm cache
$ npm cache ls
copy

Verify the integrity of the npm cache
$ npm cache verify
copy

Show the cache path
$ npm [[c|config]] get cache
copy

Change the cache path
$ npm [[c|config]] set cache [path/to/directory]
copy

SYNOPSIS

npm cache add <tarball-file | tarball-url | package-spec>
npm cache clean [--force]
npm cache ls [<pkg-spec>]
npm cache verify
npm cache rm <key>...

PARAMETERS

add
    Adds a package directly to the cache. This can be a local tarball file, a URL to a tarball, or a standard package specification (e.g., lodash@latest).

clean [--force]
    Deletes all or specific data from the cache. Since npm v5, clean requires the --force flag by default to prevent accidental cache clearing, as the cache is typically self-managing.

ls []
    Lists the contents of the cache. If a pkg-spec is provided, it lists only entries related to that package.

verify
    Checks the integrity of the cache contents. It prunes out invalid or corrupted entries and ensures all data is accounted for, repairing the cache as needed.

rm ...
    An alias for npm cache clean, which also requires --force for full cache clearing.

DESCRIPTION

npm cache provides subcommands to interact with npm's internal cache. This cache stores downloaded package tarballs and metadata, significantly speeding up subsequent installations of the same packages and enabling offline installations. It's designed to be robust and self-healing. The cache uses a content-addressable storage model, meaning that each piece of data is stored based on a cryptographic hash of its content, preventing data corruption and ensuring integrity.

CAVEATS

  • Disk Space: The cache can consume a significant amount of disk space over time, especially with many projects and dependencies.
  • --force for clean: Users accustomed to older npm versions might forget the mandatory --force flag for npm cache clean (since npm v5), leading to unexpected behavior (command doing nothing).
  • Transparency: While robust, directly manipulating the cache is generally not recommended unless you understand its internal structure, as it's designed to be self-managing.

CACHE LOCATION

The default cache directory is platform-specific but can be found by running npm config get cache. Typically, it's located in ~/.npm/_cacache on Unix-like systems.

PURPOSE OF CACHE

The primary goals of the npm cache are to accelerate package installations by reusing downloaded tarballs and to enable offline installation of packages that have been previously cached.

HISTORY

Prior to npm v5, the cache was a simpler, less robust system that could occasionally get corrupted, necessitating frequent npm cache clean operations.
With the release of npm v5 (May 2017), the caching mechanism was completely rewritten using cacache. This new system introduced content-addressable storage, self-healing capabilities, and better integrity checks, making the cache much more reliable and reducing the need for manual cleaning.

SEE ALSO

Copied to clipboard