NPM ERR Maximum call stack size exceeded (FIXED)

NPM ERR Maximum call stack size exceeded

Mar 20, 2023 | Read time 8 minutes

🔔 Table of contents

Introduction

I was recently working on a React app and ran npm install and got this error:

NPM ERR Maximum call stack size exceeded

The output of the shell error log looks like the following:

npm ERR! Linux 4.8.0-27-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! node v6.9.1
npm ERR! npm  v3.10.8

npm ERR! Maximum call stack size exceeded
npm ERR! 
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

The error can be tricky to replicate or fix because theres so many causes of this.

This error generally happens when you have too many function calls or you have reached the call stack limit from Node.

There are a few reasons behind this and in this post I will go over a few troubleshooting options you can try to fix it!

Step-by-step to fix this error

  1. Clear the NPM cache
  2. Remove node_modules and package-lock.json and reinstall/rebuild all the npm packages again
  3. Upgrade node versions
  4. Check that other programs are not locking out the node_modules folder
  5. Clear out the ~/.npmrc file!
  6. Use yarn to install your packages
  7. Check your project dependencies and its dependency tree

1. Clear the NPM cache

On occasions, it could be that the local cache that is on your machine is corrupt and this will lead to the error.

To fix this, just open up the terminal and run:

npm cache clean --force

When NPM installs modules, it keeps a cache of the downloaded files.

This will be ~/.npm on Linux, or %AppData%/npm-cache on Windows.

So when the next time you try to install the same package and its dependencies, it will go to the cache folder and not waste time downloading it again.

NPM cache is usually advertised as “self healing”, however I have found that I need to run this a few times!

2. Remove node_modules and package-lock.json and reinstall/rebuild all the npm packages again

Open up your terminal and go to the project’s root folder:

  1. Delete node_modules folder. This is where all the packages are saved.
  2. Delete the package-lock.json.
  3. Run npm install

3. Upgrade node versions

One good tool to use to manage Node versions is NVM!

To work with NVM and manage your Node versions we can do the following:

  1. For windows, we can go to the binary and install NVM located here:

https://github.com/coreybutler/nvm-windows/tags

For linux distros we can do the following:

sudo apt install curl 
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile
nvm install node 
  1. Uninstall all versions of node with the following command:

nvm uninstall <node version>

  1. Reinstall needed node version:

nvm install <node version>

To get the latest node version we can do nvm install latest

  1. Use node version just installed:

nvm use <node version>

In our case, we can use the command nvm use latest

4. Check that other programs are not locking out the node_modules folder

One cause of the error is that there are other programs running that are using the node_modules folder without you knowing.

For instance I once had this error: NPM ERR Maximum call stack size exceeded when I was using another IDE - Visual Studio.

Visual Studio was having a lock on the node_modules folder and that caused my npm install to max out the call stack!

The fix was to close my IDE (Visual Studio) and run npm install again.

5. Clear out the ~/.npmrc file!

The .npmrc file is just a configuration file that stores information for NPM such as the NPM registry Url, the NPM cache location, authentication configurations, and settings to control NPM behaviors, etc.

According to the documentation on NPM, the location of this .npmrc file can be in the following:

  • per-project config file (/path/to/my/project/.npmrc)
  • per-user config file (~/.npmrc)
  • global config file ($PREFIX/etc/npmrc)
  • npm builtin config file (/path/to/npm/npmrc)

6. Use yarn to install your packages

Steps to use Yarn and fix this error are as follows:

  1. Delete the node_modules folder in your project.
  2. Delete the package-lock.json files. You may need to back up your project first. Note: Do not delete 1package.json1. We will need this to install our packages!
  3. Run the command to install yarn: npm i yarn. (We can install globally with the -g flag)
  4. Go to the root directory of your project and run the command yarn install.

If you are already using yarn, we can use the following command:

yarn install

7. Check your project dependencies and its dependency tree

When you have a large project with a lot of dependencies (300+) and resulting in a large dependency tree, this error of “Maximum call stack size exceeded” can come up since there are too many function calls.

To approach this, we just need to install the dependent packages one-by-one.

As an example, consider the package.json file:

  "devDependencies": {
    "autoprefixer": "~6.2.2",
    "grunt": "^0.4.5",
    "grunt-browser-sync": "~2.2.0",
    "grunt-contrib-qunit": "^1.2.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-postcss": "~0.7.1",
    ...
    ...
    "grunt-sass": "~1.1.0",
    "grunt-sass-globbing": "~1.4.0",
    "grunt-svgstore": "~1.0.0"
  },

Now if we just run npm install it will go through the massive list of dependencies and try to download and install into your project folder and can lead to max’ing out the call stack.

So for this example, we can install the dependencies one-by-one. Open up the terminal and run each of the commands after each other:

npm install autoprefixer

npm install grunt

npm install grunt-browser-sync

etc, etc

In npm, the –no-bin-links flag is used to disable the creation of symbolic links for executable files in the node_modules/.bin directory. When this flag is set, npm will copy the executable files instead of creating symbolic links.

The node_modules/.bin directory is used to store executable files that are installed as part of npm packages. When you install a package that includes executable files, npm creates symbolic links to those files in the node_modules/.bin directory, so that you can run the executable files from the command line without specifying the full path to the file.

You can use the flag as such:

npm install --no-bin-links

Summary

In this post, I went over the error of NPM ERR Maximum call stack size exceeded. This error just means that NPM have had too many calls and have exceeded the limit.

The cause of this can be a lot of things, but it usually comes down to your project having a corrupt cache or node_modules folder, old versions of Node and NPM, other programs locking out your node_modules folder, and too many dependencies in your project.

To fix this we can follow the checklist of cleaning out your cache, deleting the node_modules and package-lock.json files and install again, check for programs thats locking files in your project, install dependencies one-by-one.

If these options do not work, then consider using other package managers such as yarn

👋 About the Author

G'day! I am Huy a software engineer based in Australia. I have been creating design-centered software for the last 10 years both professionally and as a passion.

My aim to share what I have learnt with you! (and to help me remember 😅)

Follow along on Twitter , GitHub and YouTube