Maximum call stack size exceeded on npm install [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
3 min

banner

# Maximum call stack size exceeded on npm install [Solved]

The "Maximum call stack size exceeded" NPM error occurs when you try to npm install a package but the installation process fails due to a glitched node_modules directory.

To solve the error, delete your node_modules directory and package-lock.json files and reinstall your dependencies.

Here are the commands for deleting node_modules and package-lock.json on macOS and Linux.

shell
# ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json

And here are the equivalent Windows commands.

shell
# ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json
Deleting the node_modules folder and package-lock.json file is necessary because they often glitch when the packages are installed using a different version of Node.js and npm than the current version.

Now, run the npm cache clean --force command to clean the cache.

shell
# ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force

npm cache clean force

The command clears the npm cache on your computer.

Now run the npm install command to install your dependencies and regenerate the node_modules and package-lock.json files.

shell
npm install

Try to rerun the installation command with the newly generated node_modules and package-lock.json files.

# Try rebuilding your dependencies

If the error persists, issue the npm rebuild command to rebuild your dependencies.

shell
npm rebuild

run npm rebuild command

The npm rebuild command is useful when you install a new version of Node.js and must recompile all your C++ addons with the new binary.

# Try running npm cache verify

The npm cache verify command verifies the contents of the cache folder and garbage collects any unneeded data.

shell
npm cache verify

The command also verifies the integrity of the cache indexes and the cached data.

npm cache verify

# Try updating your npm version

If the error persists, update npm to the latest version with the following command.

shell
npm install -g npm@latest

update npm version to latest

You might have to prefix the command with sudo (macOS and Linux) or open CMD as an administrator (Windows) if you get a permissions error.

Try to rerun the installation command after updating npm.

# Try installing the long-term supported version of Node.js

If the error persists, try downloading the long-term supported (LTS) version of Node.js.

You can download the LTS version from the official Node.js website.

Alternatively, you can use the NVM (node version manager) package, which is a utility used to manage your Node.js versions:

You can click on the NVM link that relates to you and follow the step-by-step instructions.

There is also a package called n that is used to manager your Node.js versions interactively.

Once you install the long-term supported (LTS) version of Node.js, run the following command to reinstall your dependencies using your new version.

shell
# ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force npm install

The commands delete node_modules and package-lock.json and reinstall your dependencies using your new Node.js and npm versions.

# Try deleting your .npmrc configuration file

The .npmrc file stores and manages the configuration used by npm.

The file sometimes glitches and prevents the npm install command from working properly.

You can try to either delete the .npmrc file or clear its contents.

First, create a copy of the file with a different name or make sure it doesn't contain any configuration you previously set that is important.

The file is located under ~/.npmrc on macOS and Linux.

On Windows, the file is usually under %USERPROFILE%\.npmrc or C:\Program Files\nodejs\node_modules\npm\npmrc.

For example, on Linux on macOS, you would issue the following command to create a copy of the file.

shell
cp ~/.npmrc ~/.npmrc_copy

And then remove the file with the following command.

shell
rm -f ~/.npmrc

If you don't want to remove the file, try removing its contents or most of its contents that you don't need.

Try to rerun the npm install command after cleaning up your .npmrc file.

If you can't find your .npmrc file, try issuing the npm config list command.

shell
npm config list

npm config list

The output of the command shows that my .npmrc file is located under /home/borislav/.npmrc.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev