'Failing to install Node.js dependencies on Heroku

I'm getting a memory error when trying to deploy a Node.js app to heroku. It doesn't seem to get past the installation section:

remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Node.js app detected
remote:
remote: -----> Creating runtime environment
remote:
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote:        NODE_VERBOSE=false
remote:
remote: -----> Installing binaries
remote:        engines.node (package.json):  unspecified
remote:        engines.npm (package.json):   unspecified (use default)
remote:
remote:        Resolving node version 12.x...
remote:        Downloading and installing node 12.13.1...
remote:        Using default npm version: 6.12.1
remote:
remote: -----> Restoring cache
remote:        Cached directories were not restored due to a change in version of node, npm, yarn or stack
remote:        Module installation may take longer for this build
remote:
remote: -----> Installing dependencies
remote:        Installing node modules (package.json + package-lock)
remote:        npm ERR! Maximum call stack size exceeded
remote:
remote:        npm ERR! A complete log of this run can be found in:
remote:        npm ERR!     /tmp/npmcache.TGwfg/_logs/2019-11-23T11_15_42_537Z-debug.log

I've tried turning the module cache off, which did nothing. I've also tried turning more verbose npm logging, but all that tells me is that the installation hasn't finished.

The application isn't particularly big, just an express application. There must be some dependency that has a large stack of pre/post install commands... I guess the only way to find out which one it is by uninstalling one at a time and redeploying :(

But before I go down that route, I thought I'd ask around to see if anyone else has experienced this before. I'm only using a free dyno at the moment to test if Heroku would be suitable.



Solution 1:[1]

Turns out that using yarn will help me avoid this problem.

rm package-lock.json
yarn
git add -A
git commit -m "chore: use yarn"
git push heroku master

If I decide to upgrade the service then I ask Heroku, directly, why I experienced this. Until then, this is an OK solution,

Solution 2:[2]

I had this issue and @Rashomon's solution worked, but I had to type it thusly "engines": {"node": "14.16.1", "npm": "6.14.12"}, note the double quotes on everything.

To check your versions, use npm version and node -v in command line

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 johngeorgewright
Solution 2 Robert Graham