How to fix npm ERR! write after end in npm

npmINTERMEDIATEMEDIUMUpdated Dec 5, 2025· 85 views

The 'write after end' error occurs when npm attempts to write data to a stream after it has been closed, typically during package extraction. This is often caused by npm version bugs, network issues, or corrupted cache.

Quick fix

Fixes npm ERR! write after end

npm cache verify
npm install
  1. Clear npm cache and retry
  2. Delete package-lock.json and reinstall
  3. Update npm to the latest stable version
  4. Check network connectivity and proxy settings
npm — error
npm cache verify
npm install
npm ERR! write after end

What this error means

The "npm ERR! write after end" error occurs when npm attempts to write data to a stream (file or network connection) after that stream has already been closed or ended. This is a stream handling error that happens during the package extraction phase of npm install, typically when npm's internal modules are decompressing and writing downloaded packages to disk. The error fundamentally violates Node.js stream rules: once a stream's end() method has been called, no further writes are allowed. During npm's extraction process, if the stream closes prematurely due to network issues, timing problems, or internal race conditions in older npm versions, subsequent write attempts fail with this error. This error was particularly prevalent in npm versions 5.7.0 through 6.0.x due to a stream handling bug in the pacote module.

Typical symptoms

  • npm install terminates abruptly with 'Error: write after end' message
  • Installation sometimes succeeds on retry, but fails again on subsequent attempts
  • Large projects or packages fail more frequently than small ones
  • Angular, Truffle, or other framework scaffolding commands fail during dependency installation
  • Error occurs intermittently, making it difficult to reproduce consistently

Common causes

  1. npm version regression bug—npm 5.7.0 through 6.0.x introduced a stream handling issue in the pacote module
  2. Network interruption or packet loss—unstable connections interrupting the download stream mid-extraction
  3. Corrupted or incomplete package-lock.json file—stale lockfile metadata causing incorrect stream behavior
  4. Concurrent stream write race conditions—multiple asynchronous operations writing to the same extraction stream
  5. System disk I/O throttling or storage pressure—limited disk space causing extraction to fall out of sync
  6. DNS or registry connectivity issues—temporary npm registry unavailability causing timeouts

How to fix "npm ERR! write after end"

1Clear npm cache and retry

Start with cache verification, which scans for corrupted entries:

bash
npm cache verify
npm install

If the error persists, try a harder reset:

bash
npm cache clean --force
npm install

The --force flag bypasses safety checks. Sometimes the error is transient due to partial downloads left in cache.

2Delete package-lock.json and reinstall

The package-lock.json file can become corrupted or misaligned with your npm version:

bash
rm package-lock.json
npm install

Deleting it forces npm to recalculate all dependencies from scratch. This resolves issues where the lockfile contains incorrect stream expectations.

On Windows, use del package-lock.json instead.

3Update npm to the latest stable version

Older npm versions (5.7.0 through 6.0.x) contained confirmed stream handling bugs:

bash
npm install -g npm@latest
npm cache verify
npm install

Update npm globally first, verify the cache, then retry installation. Check your current version with npm --version before upgrading.

4Check network connectivity and proxy settings

If using a proxy, corporate firewall, or VPN, these can interrupt the extraction stream. Test direct connectivity:

bash
npm ping
npm config get registry

If using a proxy, configure it explicitly:

bash
npm config set proxy http://proxy.example.com:8080
npm config set https-proxy http://proxy.example.com:8080

Or temporarily disable the proxy to test:

bash
npm config delete proxy
npm config delete https-proxy
npm install
5Increase npm timeout and set verbose logging

The default timeout may be too aggressive for large packages or slow connections:

bash
npm install --verbose --fetch-timeout=60000

Or permanently adjust the timeout:

bash
npm config set fetch-timeout 60000
npm install

Increasing it to 60 seconds gives npm more time to complete extraction before stream closure. The --verbose flag shows exactly where the process fails.

6Downgrade npm as a last resort

If all above steps fail, downgrade to npm 5.6.0, which predates the regression bug:

bash
npm install -g npm@5.6.0
npm install

Or try a specific v6 LTS version known to be stable:

bash
npm install -g npm@6.14.18
npm install

This is a temporary workaround only. Update npm again once the issue is fixed.

Advanced notes

The root cause in npm 5.7.0+ involved a timing issue where the pkgJson transform in pacote was operating asynchronously, but the tar extraction module expected synchronous stream behavior. This created a race condition where the extraction stream could close before all writes completed.

This was primarily fixed in npm 6.1.0 and later versions. However, network-related variants of this error can still occur with any npm version if the npm registry is slow, unreachable, or if your connection is unstable.

Additionally, if you're extracting very large monorepo projects with thousands of dependencies, you may hit this error due to system I/O limitations rather than npm bugs—in such cases, increasing ulimit settings and ensuring adequate disk space is critical.

Frequently asked questions

What does "npm ERR! write after end" mean?
The "npm ERR! write after end" error occurs when npm attempts to write data to a stream (file or network connection) after that stream has already been closed or ended. This is a stream handling error that happens during the package extraction phase of npm install, typically when npm's internal modules are decompressing and writing downloaded packages to disk.
How do I fix "npm ERR! write after end"?
The 'write after end' error occurs when npm attempts to write data to a stream after it has been closed, typically during package extraction. This is often caused by npm version bugs, network issues, or corrupted cache.
What causes "npm ERR! write after end"?
npm version regression bug—npm 5.7.0 through 6.0.x introduced a stream handling issue in the pacote module Network interruption or packet loss—unstable connections interrupting the download stream mid-extraction Corrupted or incomplete package-lock.json file—stale lockfile metadata causing incorrect stream behavior
Is the "npm ERR! write after end" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading