How to fix npm ERR! Maximum call stack size exceeded in npm

npmINTERMEDIATEMEDIUMUpdated Dec 6, 2025· 157 views

The "Maximum call stack size exceeded" error typically indicates circular dependencies, corrupted cache, or npm version bugs. Clear cache, update npm, and check for dependency cycles.

Quick fix

Fixes npm ERR! Maximum call stack size exceeded

# Remove dependencies
rm -rf node_modules package-lock.json

# Clean npm cache
npm cache clean --force

# Reinstall
npm install
  1. Clear cache and reinstall
  2. Update npm to latest version
  3. Check .npmrc for issues
  4. Fix directory path issues
npm — error
# Remove dependencies
rm -rf node_modules package-lock.json
# Clean npm cache
npm cache clean --force
# Reinstall
npm install
npm ERR! Maximum call stack size exceeded

What this error means

This error occurs when npm's internal function calls exceed JavaScript's stack limit, causing a stack overflow. This isn't a problem with your code—it's npm's dependency resolver entering infinite recursion. The most common cause is circular dependencies (package A depends on B, B depends on A), but it can also happen with corrupted cache, expired authentication tokens for private registries, or bugs in older npm versions.

Typical symptoms

  • npm install fails with "Maximum call stack size exceeded"
  • Error occurs immediately after npm update or Node version switch
  • npm audit fix fails in monorepo/workspace setups
  • CI builds fail while local development may succeed
  • No useful error message beyond the stack size error
  • Error appears after upgrading dependencies

Common causes

  1. Circular dependencies between packages
  2. Corrupted npm cache
  3. Expired or invalid .npmrc credentials for private registries
  4. npm version bugs (especially npm v5.x)
  5. Spaces in project directory path
  6. Docker WORKDIR set to root (/)
  7. IDE file locking preventing node_modules access

How to fix "npm ERR! Maximum call stack size exceeded"

1Clear cache and reinstall

Reset npm state completely:

bash
# Remove dependencies
rm -rf node_modules package-lock.json

# Clean npm cache
npm cache clean --force

# Reinstall
npm install
2Update npm to latest version

Older npm versions have known bugs:

bash
# Update npm globally
npm install -g npm@latest

# Verify version
npm -v

# Try again
npm install

Use npm 9+ for best results.

3Check .npmrc for issues

Review authentication and configuration:

bash
# View .npmrc contents
cat ~/.npmrc

# Look for:
# - Expired registry tokens
# - Invalid proxy settings
# - Malformed entries

# Remove problematic .npmrc temporarily
mv ~/.npmrc ~/.npmrc.backup
npm install
4Fix directory path issues

Spaces in paths can cause problems:

bash
# Bad: /path with spaces/project/
# Good: /path-no-spaces/project/

# Move project to path without spaces
mv "/path with spaces/project" /path-no-spaces/project
cd /path-no-spaces/project
npm install

For Docker, use proper WORKDIR:

dockerfile
WORKDIR /app  # Good
# WORKDIR /   # Bad - causes conflicts
5Close IDE and retry

IDEs can lock node_modules files:

bash
# Close VS Code, WebStorm, etc.

# Then run in terminal
npm install

# Reopen IDE after install completes
6Use npm ci instead

npm ci has less overhead:

bash
# Clean install from lockfile
npm ci

This reduces dependency resolution complexity.

7Increase Node stack size (workaround)

Temporary fix while investigating root cause:

bash
# Increase stack size
export NODE_OPTIONS='--stack-size=10000'
npm install

Note: This is a workaround, not a permanent solution.

Advanced notes

For monorepos and workspaces, circular dependency issues are amplified. Use tools like madge to detect circular dependencies:

bash
npx madge --circular src/

In CI/CD environments, ensure WORKDIR in Docker is not root (/) and use npm ci instead of npm install.

If using private npm registries, authentication tokens expire. Check with your registry administrator and regenerate tokens if needed.

The error "E401: Incorrect or missing password" locally may appear as "Maximum call stack size exceeded" in CI due to recursive authentication attempts.

Frequently asked questions

What does "npm ERR! Maximum call stack size exceeded" mean?
This error occurs when npm's internal function calls exceed JavaScript's stack limit, causing a stack overflow. This isn't a problem with your code—it's npm's dependency resolver entering infinite recursion. The most common cause is circular dependencies (package A depends on B, B depends on A), but it can also happen with corrupted cache, expired authentication tokens for private registries, or bugs in older npm versions.
How do I fix "npm ERR! Maximum call stack size exceeded"?
The "Maximum call stack size exceeded" error typically indicates circular dependencies, corrupted cache, or npm version bugs. Clear cache, update npm, and check for dependency cycles.
What causes "npm ERR! Maximum call stack size exceeded"?
Circular dependencies between packages Corrupted npm cache Expired or invalid .npmrc credentials for private registries
Is the "npm ERR! Maximum call stack size exceeded" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading