npm’s offline mode lets you install packages even when you’re completely disconnected from the internet, which sounds like magic but is actually a clever caching mechanism.
Let’s see it in action. Imagine you’ve just cloned a project that uses lodash and express, and you’re on a plane.
$ cd /path/to/my/project
$ npm install
Normally, this would fail immediately if there’s no internet. But if you’ve run npm install before on any machine with a network connection, those packages are likely in your npm cache.
The core idea behind npm’s offline mode is the cache. When you run npm install, npm doesn’t just download packages directly from the registry and then delete them. Instead, it downloads them into a local cache directory on your machine. This cache is a treasure trove of previously downloaded packages.
Here’s how it works internally:
- Fetch Request: When
npm installis run, npm first checks its cache for the requested package and version. - Cache Hit: If the package is found in the cache, npm uses that version directly, avoiding any network request. This is the "offline mode" magic.
- Cache Miss: If the package is not found in the cache, npm then attempts to download it from the registry.
- Cache Storage: Once downloaded from the registry, the package is stored in the cache before being installed in your
node_modulesdirectory.
The exact location of your npm cache depends on your operating system:
- Linux/macOS:
~/.npm - Windows:
%AppData%/npm-cache
You can see the cache directory by running:
$ npm config get cache
This cache is shared across all your npm projects on that machine. So, if you install lodash in one project, the next time any project on that machine needs lodash (and the version matches), it will be served from the cache.
The primary lever you control is implicitly: keep your cache populated. The more you install packages with an internet connection, the more comprehensive your offline cache becomes.
To explicitly clean or manage your cache, you can use these commands:
-
Clean the entire cache:
$ npm cache clean --forceThis command removes all packages from your cache. Use it with caution, as it means the next
npm installwill have to re-download everything. -
Verify cache integrity:
$ npm cache verifyThis command checks the integrity of your cache. It removes any corrupted or extraneous files. It’s a good maintenance command to run periodically.
The mental model to build is that npm install is primarily a cache lookup followed by a registry download and cache population if necessary. The "offline mode" is simply the scenario where the cache lookup is sufficient for all requested packages.
When you’re working in an environment with intermittent or no internet, the key is to have previously run npm install in a connected environment to "seed" your cache. If you’re setting up a new project on a disconnected machine, you’ll need to connect it to the internet at least once to npm install all its dependencies to populate the cache for future offline use.
The real surprise is how robust this cache is. It’s not just a simple file store; npm uses hashing and metadata to ensure package integrity and efficient retrieval. It’s designed to be resilient, so even if a download was interrupted, the partial file might be there, and npm cache verify can clean it up. This resilience means you can often get away with less-than-perfect network conditions during your initial installs, and subsequent offline installs will be smooth.
The next challenge you’ll often face is managing different npm registries, especially when working with private packages.