Heroku 服务器在部署“react-admin”应用程序时因“JavaScript 堆内存不足”而崩溃

问题描述 投票:0回答:9

我目前正在使用“react-admin”开发一个管理面板,它在我的本地运行良好,但是一旦我将应用程序上传到 Heroku,构建就会失败并出现以下错误:“致命错误:堆附近的标记压缩无效限制分配失败 - JavaScript 堆内存不足”

我通过设置一个干净的项目,只使用“react-admin”包和简单导入 Admin,将问题缩小到最初的

import { Admin } from 'react-admin';
。一旦我尝试使用该组件,部署就会因“堆内存不足”错误而失败。

还有其他人遇到过这个问题吗?

这是我的 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"
    ]
  }
}

这里是 Heroku 日志输出:

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! [email protected] 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 [email protected] 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

谢谢!

node.js heroku memory heap-memory react-admin
9个回答
76
投票

联系heroku支持,这里我解决了这个问题:

错误发生是因为节点在遇到 JavaScript 堆内存不足错误之前只能寻址 1.5GB 内存,这是它从 V8 引擎继承的默认限制。我们通过 NODE_OPTIONS env var 在 Heroku 上调整此默认设置,以便进程可以寻址所有可用内存:https://github.com/heroku/heroku-buildpack-nodejs/blob/master/lib/environment.sh #L29-L33

一个选项是在调用节点时专门调用 --max_old_space_size=2560 以查看是否有助于缓解问题。您可以通过运行以下命令通过环境变量将该选项传递给 Node:

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

同时将构建脚本调整为:

"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"
  }

虽然不太确定构建和启动脚本的变化会产生影响。


8
投票

如果您使用的是免费的 dyno 版本。请将您的 package.json start 脚本更改为:

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

更多信息在这里:https://devcenter.heroku.com/articles/node-memory-use


7
投票

错误发生是因为节点在遇到 JavaScript 堆内存不足错误之前只能寻址 1.5GB 内存,这是它从 V8 引擎继承的默认限制。我们通过 NODE_OPTIONS env var 调整 Heroku 上的默认设置,以便进程可以寻址所有可用内存。

您可以调整大小:

  1. 给出命令(heroku config:set NODE_OPTIONS='--max_old_space_size=2560 [app-name]'
  2. 在“Config Vars”部分添加一个新的配置变量 SEE THIS IMAGE!

5
投票

"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" }

不确定是否需要更改脚本。在部署期间,内存使用量跃升至 1Gb
以上


3
投票
Inovramadani

答案并试图改进那个答案,但是当我发现其中有多个问题时,我决定给出自己的答案。在 Heroku 日志上,我也收到此错误消息说 Node 不允许 -a flag,所以正确的命令是

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

    


2
投票

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

您还可以:

如果在运行时不需要,则将不必要的依赖项移至 devDependencies。
  1. heroku restart
  2. 重新开始。
  3. heroku logs --tail
  4. 查看日志。
    
        

0
投票

我刚刚降级了一些属性的版本:

"react-dom"

"^16.13.0"
"react-scripts"
"3.4.0"
package.json
文件中,在 Heroku 中像魅力一样工作。
原始解决方案来源:

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


0
投票

这是通过通过 heroku cli 运行以下命令将您的 heroku 实例更改为容器来完成的

heroku stack:set container;

然后添加一个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" ]

和一个 heroku.yml

#heroku.yml build: docker: web: dockerfile



0
投票

姓名:

NODE_OPTIONS

价值:

--max_old_space_size=2560

    

© www.soinside.com 2019 - 2024. All rights reserved.