LinuxCommandLibrary

npm-cache

Manages the npm package cache

TLDR

Add a specific package to the cache

$ npm cache add [package_name]
copy

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

Clear the entire npm cache
$ npm cache clean [[-f|--force]]
copy

List cached packages
$ npm cache ls
copy

List cached packages matching a specific name and version
$ npm cache ls [name]@[version]
copy

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

List all entries in the npx cache
$ npm cache npx ls
copy

SYNOPSIS

npm cache add <path>
npm cache clean [--force]
npm cache verify

PARAMETERS

add <path>
    Add specified tarball path to the cache

clean [--force]
    Remove all cache data; --force required since npm 5.0.0

verify
    Check cache integrity, remove invalid/unneeded files

--cache <path>
    Set custom cache directory

--force
    Bypass safety checks for clean operation

DESCRIPTION

The npm cache command manages the directory where npm stores downloaded package tarballs to accelerate future installations. This cache prevents redundant downloads, improving efficiency across projects.

Key subcommands include add, which manually places a tarball into the cache; clean, which removes all cache data to free disk space; and verify, which scans for corruption, removes invalid files, and ensures consistency.

By default on Linux, the cache resides at ~/.npm, which can expand significantly with heavy usage. Cleaning does not impact node_modules directories or global installs—only cached archives. Since npm 5.0.0, clean requires --force for safety. The --cache option overrides the location, useful for custom setups or CI environments.

This command is essential for maintenance, troubleshooting install issues, or managing storage on cache-heavy systems.

CAVEATS

Cleaning cache triggers re-downloads on next install, increasing bandwidth use. Does not affect installed packages. Use verify first for partial cleanup.

DEFAULT CACHE PATH

Linux/Unix: ~/.npm
View with npm config get cache

CACHE SIZING

Can grow to GBs; monitor with du -sh ~/.npm
Regular verify prevents bloat without full clean.

HISTORY

Part of npm since v1.x; npm 5.0.0 (2017) added --force requirement for clean to prevent data loss. Evolved with scoped packages and better verification in npm 6+.

SEE ALSO

npm(1)

Copied to clipboard