47

I am currently developing an admin panel using 'react-admin' which works well on my local, but as soon as I upload the app to Heroku, the build fails with the following error: "FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory"

I have narrowed down the issue to the initial import { Admin } from 'react-admin'; by setting up a clean project with just the 'react-admin' package and a simple import of Admin. As soon as I try using the component the deploy will fail with a "heap out of memory" error.

Has anybody else encountered this issue?

Here is my package.json:

{
  "name": "mrewards_admin",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.19.0",
    "deepmerge": "^4.2.2",
    "prop-types": "^15.7.2",
    "react": "^16.12.0",
    "react-admin": "^3.0.2",
    "react-dom": "^16.12.0",
    "react-scripts": "3.2.0"
  },
  "engines": {
    "node": "13.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

And here is the Heroku log output:

2019-12-06T00:29:07.000000+00:00 app[api]: Build succeeded
2019-12-06T00:29:10.340460+00:00 heroku[web.1]: State changed from starting to up
2019-12-06T00:29:10.135608+00:00 app[web.1]: ℹ 「wds」: Project is running at http://172.18.103.22/
2019-12-06T00:29:10.135999+00:00 app[web.1]: ℹ 「wds」: webpack output is served from /
2019-12-06T00:29:10.136098+00:00 app[web.1]: ℹ 「wds」: Content not from webpack is served from /app/public
2019-12-06T00:29:10.136196+00:00 app[web.1]: ℹ 「wds」: 404s will fallback to /index.html
2019-12-06T00:29:10.136405+00:00 app[web.1]: Starting the development server...
2019-12-06T00:29:10.136409+00:00 app[web.1]:
2019-12-06T00:30:46.302146+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=test-react-admin.herokuapp.com request_id=b7cafb3a-311c-4ef2-bd61-cb1f863e69ca fwd="216.81.49.130" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=https
2019-12-06T00:30:47.234363+00:00 app[web.1]:
2019-12-06T00:30:47.234390+00:00 app[web.1]: <--- Last few GCs --->
2019-12-06T00:30:47.234392+00:00 app[web.1]:
2019-12-06T00:30:47.234413+00:00 app[web.1]: [30:0x492c940]    98675 ms: Scavenge 240.7 (257.5) -> 239.8 (257.7) MB, 1.7 / 0.0 ms  (average mu = 0.238, current mu = 0.037) allocation failure
2019-12-06T00:30:47.234415+00:00 app[web.1]: [30:0x492c940]    98685 ms: Scavenge 240.7 (257.7) -> 239.9 (258.0) MB, 1.9 / 0.0 ms  (average mu = 0.238, current mu = 0.037) allocation failure
2019-12-06T00:30:47.234418+00:00 app[web.1]: [30:0x492c940]    98903 ms: Mark-sweep 240.8 (258.0) -> 237.9 (258.0) MB, 211.0 / 0.1 ms  (average mu = 0.350, current mu = 0.474) allocation failure scavenge might not succeed
2019-12-06T00:30:47.234419+00:00 app[web.1]:
2019-12-06T00:30:47.234421+00:00 app[web.1]:
2019-12-06T00:30:47.234422+00:00 app[web.1]: <--- JS stacktrace --->
2019-12-06T00:30:47.234423+00:00 app[web.1]:
2019-12-06T00:30:47.234426+00:00 app[web.1]: ==== JS stack trace =========================================
2019-12-06T00:30:47.234427+00:00 app[web.1]:
2019-12-06T00:30:47.234429+00:00 app[web.1]: 0: ExitFrame [pc: 0x1374fd9]
2019-12-06T00:30:47.234431+00:00 app[web.1]: Security context: 0x2a1cf61808a1 <JSObject>
2019-12-06T00:30:47.234433+00:00 app[web.1]: 1: _next2(aka _next2) [0x2546e8465cf9] [0x1b716e9004a9 <undefined>:~21] [pc=0x1d633e40a0e8](this=0x1b716e9004a9 <undefined>)
2019-12-06T00:30:47.234440+00:00 app[web.1]: 2: /* anonymous */(aka /* anonymous */) [0x2546e8465ea1] [0x1b716e9004a9 <undefined>:46] [bytecode=0x6238003a241 offset=37](this=0x1b716e9004a9 <undefined>,0x1b716e9004a9 <undefined>,0x1b716e9004a9 <undefined>)
2019-12-06T00:30:47.234441+00:00 app[web.1]: 3: _next1(aka _ne...
2019-12-06T00:30:47.234443+00:00 app[web.1]:
2019-12-06T00:30:47.234445+00:00 app[web.1]: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
2019-12-06T00:30:47.234453+00:00 app[web.1]:
2019-12-06T00:30:47.245340+00:00 app[web.1]: Writing Node.js report to file: report.20191206.003047.30.0.001.json
2019-12-06T00:30:47.245345+00:00 app[web.1]: Node.js report completed
2019-12-06T00:30:47.245828+00:00 app[web.1]: 1: 0x9da7c0 node::Abort() [node]
2019-12-06T00:30:47.246377+00:00 app[web.1]: 2: 0x9db976 node::OnFatalError(char const*, char const*) [node]
2019-12-06T00:30:47.246963+00:00 app[web.1]: 3: 0xb39f1e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
2019-12-06T00:30:47.247418+00:00 app[web.1]: 4: 0xb3a299 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
2019-12-06T00:30:47.247992+00:00 app[web.1]: 5: 0xce5635  [node]
2019-12-06T00:30:47.248561+00:00 app[web.1]: 6: 0xce5cc6 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]
2019-12-06T00:30:47.249102+00:00 app[web.1]: 7: 0xcf1b5a v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
2019-12-06T00:30:47.249627+00:00 app[web.1]: 8: 0xcf2a65 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
2019-12-06T00:30:47.250190+00:00 app[web.1]: 9: 0xcf5478 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationAlignment) [node]
2019-12-06T00:30:47.250701+00:00 app[web.1]: 10: 0xcbbda7 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType) [node]
2019-12-06T00:30:47.251301+00:00 app[web.1]: 11: 0xff1e0b v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
2019-12-06T00:30:47.251959+00:00 app[web.1]: 12: 0x1374fd9  [node]
2019-12-06T00:30:47.285408+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-12-06T00:30:47.285920+00:00 app[web.1]: npm ERR! errno 1
2019-12-06T00:30:47.287473+00:00 app[web.1]: npm ERR! ra-test@0.1.0 start: `react-scripts start`
2019-12-06T00:30:47.287759+00:00 app[web.1]: npm ERR! Exit status 1
2019-12-06T00:30:47.288057+00:00 app[web.1]: npm ERR!
2019-12-06T00:30:47.288300+00:00 app[web.1]: npm ERR! Failed at the ra-test@0.1.0 start script.
2019-12-06T00:30:47.288528+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-12-06T00:30:47.295555+00:00 app[web.1]:
2019-12-06T00:30:47.295669+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-12-06T00:30:47.295759+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-12-06T00_30_47_289Z-debug.log
2019-12-06T00:30:47.344869+00:00 heroku[web.1]: Process exited with status 1
2019-12-06T00:30:47.369971+00:00 heroku[web.1]: State changed from up to crashed

Thank you!

trincot
  • 317,000
  • 35
  • 244
  • 286
Vladimir_Grigore
  • 521
  • 1
  • 4
  • 8
  • I just had the same experience trying to deploy with Google App Engine so definitely not a Heroku issue. – Vladimir_Grigore Dec 06 '19 at 18:50
  • This worked for me [Error with running ReactJS/TailwindCSS/Heroku: React Refresh runtime should not be included in the production bundle](https://stackoverflow.com/questions/65623606/error-with-running-reactjs-tailwindcss-heroku-react-refresh-runtime-should-not) – sudonitin Aug 28 '21 at 11:22
  • This post helped me resolve my issue [Error with running ReactJS/TailwindCSS/Heroku: React Refresh runtime should not be included in the production bundle](https://stackoverflow.com/questions/65623606/error-with-running-reactjs-tailwindcss-heroku-react-refresh-runtime-should-not) – sudonitin Aug 28 '21 at 15:37
  • This post helped me [Error with running ReactJS/TailwindCSS/Heroku: React Refresh runtime should not be included in the production bundle](https://stackoverflow.com/questions/65623606/error-with-running-reactjs-tailwindcss-heroku-react-refresh-runtime-should-not) – sudonitin Aug 28 '21 at 15:38

9 Answers9

79

Contacted heroku support, here I solved the issue:

The error has occurred because the node can only address 1.5GB of memory before hitting the JavaScript heap out of memory error, a default limit that it inherits from the V8 engine. We tweak this default setting on Heroku through the NODE_OPTIONS env var so that the process can address all of the memory available: https://github.com/heroku/heroku-buildpack-nodejs/blob/master/lib/environment.sh#L29-L33

One option would be to specifically call --max_old_space_size=2560 when invoking node to see if that helps alleviate the issue. You can pass that option to Node via environment variable by running the following:

$ heroku config:set NODE_OPTIONS="--max_old_space_size=2560" -a [app_name]

Also adjust the build script to be:

"scripts": {
    "start": "node --max_old_space_size=2560 node_modules/.bin/react-scripts start",
    "build": "node --max_old_space_size=2560 node_modules/.bin/react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }

Though not really sure build and start scripts change impacting.

MalcolmOcean
  • 2,807
  • 2
  • 29
  • 38
Inovramadani
  • 1,955
  • 3
  • 14
  • 34
  • 1
    I tried this and it worked! thx. I don't think the build script adjustments are necessary though because running the `heroku config` command through the cli updates the config vars associated with your app between boots (which you can see from the heroku settings tab https://devcenter.heroku.com/articles/config-vars#using-the-heroku-dashboard) – Pirijan Feb 05 '20 at 04:11
  • on my mac, when I used this command it led to `dquote> `, also I visited heroku settings tab and no config variable was added there, please tell what is this dquote?? – Muhammad Awais Feb 03 '21 at 10:34
  • 1
    correction is needed in your command, small commas issue not major issue, correct command is `heroku config:set NODE_OPTIONS='--max_old_space_size=2560 -a [app-name]'` – Muhammad Awais Feb 03 '21 at 11:06
  • (I've now edited the post to remove the extraneous trailing double-quote.) – MalcolmOcean Apr 26 '21 at 08:28
  • 1
    This did it for me, thank you so much. Learned something new. – kevin Sep 19 '21 at 01:00
  • i wasted 7 hours for this issue and this answer fixed it – Wasif Khalil Oct 01 '22 at 16:45
  • it works. Increased to 3000 – Lotpite Dec 22 '22 at 12:56
8

If you are using the free dyno version. Please, change your package.json start script to:

"start": "node --optimize_for_size --max_old_space_size=460 dist/main.js",

More Info here: https://devcenter.heroku.com/articles/node-memory-use

Hasan Ahamed
  • 186
  • 3
  • 6
7

The error has occurred because the node can only address 1.5GB of memory before hitting the JavaScript heap out of memory error, a default limit that it inherits from the V8 engine. We tweak this default setting on Heroku through the NODE_OPTIONS env var so that the process can address all of the memory available.

You can resize:

  1. giving the command (heroku config:set NODE_OPTIONS='--max_old_space_size=2560 [app-name]')
  2. adding a new config variable into 'Config Vars' section (go to your app in heroku dashboard -> Settings -> Config Vars) SEE THIS IMAGE!
Andrei
  • 71
  • 1
  • 4
5

The only way I was able to make this work was by upgrading to a paid dyno and changing the scripts in my package.json to:

"scripts": {
    "start": "node --max_old_space_size=2048 node_modules/.bin/react-scripts start",
    "build": "node --max_old_space_size=2048 node_modules/.bin/react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }

Not sure if the script changes were needed. During deployment the memory usage jumps above 1Gb

Vladimir_Grigore
  • 521
  • 1
  • 4
  • 8
3

first I commented on Inovramadani answer and tried to improve that one, but when I found more than one issues in that then I decided to give my own answer. On Heroku logs, I was also getting this error saying Node doesn't allow -a flag, so correct command comes out to be

heroku config:set NODE_OPTIONS='--max_old_space_size=2560 [app-name]'

Muhammad Awais
  • 1,608
  • 1
  • 21
  • 21
  • Why is [app-name] inside the quotes and not outside? – prototype Feb 25 '21 at 18:59
  • it should be inside of quotes, but obviously you will replace [app-name] with your original app name inside the quotes – Muhammad Awais Feb 25 '21 at 19:06
  • 3
    pretty sure this is confused; `-a [app-name]` is part of the `heroku config:set` command itself, not part of the value. the other answer has an extra `"` at the end, which I'll fix – MalcolmOcean Apr 26 '21 at 01:00
3

I disabled Heroku node_modules cache and worked.

heroku config:set NODE_MODULES_CACHE=false
git commit -am 'disable node_modules cache' --allow-empty
git push heroku

You can also:

  1. Move unnecessary dependencies to devDependencies if not needed on runtime.
  2. Restart with heroku restart.
  3. heroku logs --tail to see logs.
Nagibaba
  • 4,218
  • 1
  • 35
  • 43
1

Based on the other answers I simply needed to add the following config var, there was no need for changing scripts:

Name: NODE_OPTIONS

Value: --max_old_space_size=2560

thisismydesign
  • 21,553
  • 9
  • 123
  • 126
0

I found a perfect solution for me.

I have just downgraded the versions of some attributes with: "react-dom": "^16.13.0" and "react-scripts": "3.4.0" in package.json file and works like charm in Heroku.

Source of original solution: https://bismobaruno.medium.com/fixing-memory-heap-reactjs-on-heroku-16910e33e342

0

You could also try running your React app within a docker container. I'm not confident in why, but this worked for me

This is done by changing your heroku instance to a container by running the following through the heroku cli

heroku stack:set container; 

Then adding a dockerfile

#dockerfile
# syntax=docker/dockerfile:1
FROM node:latest
WORKDIR /app

COPY ["package.json", "package-lock.json*", "./"]
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
#Either run in normally
CMD ["npm","start"]

#or run in production
#RUN npm install -g serve
#CMD [ "serve", "-s", "build" ]

and a heroku.yml

#heroku.yml
build:
  docker:
    web: dockerfile
Josh
  • 1