Last updated: Apr 4, 2024
Reading timeยท3 min
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.
# ๐๏ธ (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.
# ๐๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json
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.
# ๐๏ธ clean your npm cache 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.
npm install
Try to rerun the installation command with the newly generated node_modules
and package-lock.json
files.
If the error persists, issue the npm rebuild command to rebuild your dependencies.
npm rebuild
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.
npm cache verify
The npm cache verify
command verifies the contents of the cache folder and
garbage collects any unneeded data.
npm cache verify
The command also verifies the integrity of the cache indexes and the cached data.
If the error persists, update npm
to the latest version with the following
command.
npm install -g npm@latest
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
.
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.
# ๐๏ธ (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.
.npmrc
configuration fileThe .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.
cp ~/.npmrc ~/.npmrc_copy
And then remove the file with the following command.
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.
npm config list
The output of the command shows that my .npmrc
file is located under
/home/borislav/.npmrc
.