Elastic Beanstalk(t2.small)部署npm install内存不足报错求助
问题描述
在规格为t2.small(内存2GB)的Elastic Beanstalk实例上部署应用,执行npm install安装依赖时触发JavaScript堆内存不足错误。当前使用Node.js 16.17.0和npm 8.15.0,已为实例添加1GB交换分区(swap)但问题仍未解决。
报错日志
2022/09/27 11:41:10.209208 [INFO] npm WARN config production Use `--omit=dev` instead. <--- Last few GCs ---> [3318:0x55f9fa0] 74664 ms: Scavenge 971.9 (1014.0) -> 970.7 (1014.0) MB, 22.0 / 0.0 ms (average mu = 0.755, current mu = 0.702) allocation failure [3318:0x55f9fa0] 74722 ms: Scavenge 972.7 (1014.0) -> 971.8 (1014.0) MB, 7.7 / 0.0 ms (average mu = 0.755, current mu = 0.702) allocation failure [3318:0x55f9fa0] 74780 ms: Scavenge 974.4 (1014.0) -> 973.3 (1022.0) MB, 6.5 / 0.0 ms (average mu = 0.755, current mu = 0.702) allocation failure <--- JS stacktrace ---> FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory 1: 0xb02930 node::Abort() [npm install] 2: 0xa18149 node::FatalError(char const*, char const*) [npm install] 3: 0xcdd16e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [npm install] 4: 0xcdd4e7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [npm install] 5: 0xe94b55 [npm install] 6: 0xea481d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [npm install] 7: 0xea751e v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [npm install] 8: 0xe68a5a v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [npm install] 9: 0x11e17c6 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [npm install] 10: 0x15d5439 [npm install] 2022/09/27 11:41:10.211702 [ERROR] An error occurred during execution of command [app-deploy] - [Use NPM to install dependencies]. Stop running the command. Error: Command /bin/sh -c npm --production install failed with error signal: aborted. Stderr:npm WARN config production Use `--omit=dev` instead. <--- Last few GCs ---> [3318:0x55f9fa0] 74664 ms: Scavenge 971.9 (1014.0) -> 970.7 (1014.0) MB, 22.0 / 0.0 ms (average mu = 0.755, current mu = 0.702) allocation failure [3318:0x55f9fa0] 74722 ms: Scavenge 972.7 (1014.0) -> 971.8 (1014.0) MB, 7.7 / 0.0 ms (average mu = 0.755, current mu = 0.702) allocation failure [3318:0x55f9fa0] 74780 ms: Scavenge 974.4 (1014.0) -> 973.3 (1022.0) MB, 6.5 / 0.0 ms (average mu = 0.755, current mu = 0.702) allocation failure <--- JS stacktrace ---> FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory 1: 0xb02930 node::Abort() [npm install] 2: 0xa18149 node::FatalError(char const*, char const*) [npm install] 3: 0xcdd16e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [npm install] 4: 0xcdd4e7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [npm install] 5: 0xe94b55 [npm install] 6: 0xea481d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [npm install] 7: 0xea751e v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [npm install] 8: 0xe68a5a v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [npm install] 9: 0x11e17c6 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [npm install] 10: 0x15d5439 [npm install]
项目依赖列表
"dependencies": { "@emotion/react": "^11.10.0", "@emotion/styled": "^11.10.0", "@material-ui/core": "^4.12.4", "@material-ui/lab": "^4.0.0-alpha.61", "@mui/icons-material": "^5.10.3", "@mui/material": "^5.10.3", "@testing-library/jest-dom": "^5.11.9", "@testing-library/react": "^11.2.5", "@testing-library/user-event": "^12.8.3", "axios": "^0.27.2", "leaflet": "^1.6.0", "leaflet-control-geocoder": "^2.2.0", "leaflet.markercluster": "^1.5.3", "react": "^17.0.1", "react-dom": "^17.0.1", "react-html5-camera-photo": "^1.5.9", "react-leaflet": "^3.2.0", "react-leaflet-cluster": "^1.0.4", "react-loader-spinner": "^4.0.0", "react-redux": "^8.0.2", "react-scripts": "^5.0.1", "react-toastify": "^9.0.8", "redux": "^4.2.0", "styled-components": "^5.2.1", "styled-media-query": "^2.1.2", "styled-reset": "^4.3.4", "web-vitals": "^1.1.1" }
内存使用情况

解决方案
以下是几种针对性的解决方法:
1. 扩展Node.js堆内存上限
执行安装命令时手动指定堆内存分配,避免默认限制:
NODE_OPTIONS="--max-old-space-size=1536" npm install --omit=dev
注:1536代表分配1.5GB堆内存,可根据实例剩余内存调整,不要超过物理内存+swap的总和。
2. 优化依赖安装流程
- 替换npm为yarn:yarn内存管理更高效,执行
yarn install --production替代npm命令。 - 清理缓存:先执行
npm cache clean --force清理旧缓存,再重新安装。 - 拆分依赖:将
@testing-library/*这类仅开发环境需要的依赖移到devDependencies中,生产环境安装时自动跳过,减少内存占用。
3. 扩容swap分区
当前1GB swap不足以支撑安装需求,扩容到2GB:
# 停止当前swap sudo swapoff /swapfile # 创建2GB swap文件 sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile # 启用swap sudo swapon /swapfile # 设置开机自动挂载 echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
4. 配置Elastic Beanstalk全局参数
在项目根目录的.ebextensions文件夹下创建00_node_options.config文件,设置全局Node.js参数:
option_settings: - namespace: aws:elasticbeanstalk:application:environment option_name: NODE_OPTIONS value: --max-old-space-size=1536
同时可开启弹性Beanstalk的增量部署,避免每次重新安装所有依赖。
5. 升级实例规格
若以上方法均无效,直接将实例升级到t2.medium(4GB内存),从根源解决内存不足问题。
内容的提问来源于stack exchange,提问作者Bil11
相关产品推荐
相关产品推荐

