How to fix npm ERR! Maximum call stack size exceeded in npm
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- Clear cache and reinstall
- Update npm to latest version
- Check .npmrc for issues
- Fix directory path issues
# Remove dependenciesrm -rf node_modules package-lock.json# Clean npm cachenpm cache clean --force# Reinstallnpm installnpm 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
- Circular dependencies between packages
- Corrupted npm cache
- Expired or invalid .npmrc credentials for private registries
- npm version bugs (especially npm v5.x)
- Spaces in project directory path
- Docker WORKDIR set to root (/)
- IDE file locking preventing node_modules access
How to fix "npm ERR! Maximum call stack size exceeded"
Reset npm state completely:
# Remove dependencies
rm -rf node_modules package-lock.json
# Clean npm cache
npm cache clean --force
# Reinstall
npm installOlder npm versions have known bugs:
# Update npm globally
npm install -g npm@latest
# Verify version
npm -v
# Try again
npm installUse npm 9+ for best results.
Review authentication and configuration:
# View .npmrc contents
cat ~/.npmrc
# Look for:
# - Expired registry tokens
# - Invalid proxy settings
# - Malformed entries
# Remove problematic .npmrc temporarily
mv ~/.npmrc ~/.npmrc.backup
npm installSpaces in paths can cause problems:
# 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 installFor Docker, use proper WORKDIR:
WORKDIR /app # Good
# WORKDIR / # Bad - causes conflictsIDEs can lock node_modules files:
# Close VS Code, WebStorm, etc.
# Then run in terminal
npm install
# Reopen IDE after install completesnpm ci has less overhead:
# Clean install from lockfile
npm ciThis reduces dependency resolution complexity.
Temporary fix while investigating root cause:
# Increase stack size
export NODE_OPTIONS='--stack-size=10000'
npm installNote: 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:
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
- 📚npm Official Documentation
- 💬Stack Overflow: maximum call stack size exceeded on npm install(Stack Overflow)
- 🔗
- 🐙GitHub #1918: npm/cli(GitHub)