Heroku 在导入“aws-sdk”时崩溃
Posted
技术标签:
【中文标题】Heroku 在导入“aws-sdk”时崩溃【英文标题】:Heroku crashes when importing 'aws-sdk' 【发布时间】:2020-10-18 00:53:30 【问题描述】:我有 typescript node.js 应用程序,我在其中使用 Amazon S3。它已部署到 Heroku。
import express from 'express';
import cors from 'cors';
//import aws from 'aws-sdk';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors());
const PORT = process.env.PORT || 3001;
app.get('/ping', (request, response) =>
console.log('ping');
response.send('pong');
);
app.listen(PORT, () =>
console.log(`Server running on port $PORT`);
);
问题是如果我取消注释 import aws from 'aws-sdk'
Heroku 会崩溃并出现以下错误:
2020-06-27T14:14:53.383764+00:00 app[web.1]: 0: ExitFrame [pc: 0x13c9819]
2020-06-27T14:14:53.383764+00:00 app[web.1]: 1: StubFrame [pc: 0x134f9b7]
2020-06-27T14:14:53.383764+00:00 app[web.1]: Security context: 0x331fe51408d1 <JSObject>
2020-06-27T14:14:53.383765+00:00 app[web.1]: 2: scanRange [0x3162672e4a49] [/app/node_modules/typescript/lib/typescript.js:~10360] [pc=0x5cca21f73b2](this=0x0635f86bb221 <Object map = 0x10a03640a1b9>,604998,223,0x3ae8c05743c9 <JSFunction (sfi = 0x2064a696fc31)>)
2020-06-27T14:14:53.383766+00:00 app[web.1]: 3: addJSDocComment(aka addJSDocComment) [0x635f86bb469] [/app/node_modules/typescript/lib/typescript.js:~1...
2020-06-27T14:14:53.383766+00:00 app[web.1]:
2020-06-27T14:14:53.383799+00:00 app[web.1]: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - javascript heap out of memory
2020-06-27T14:14:53.384587+00:00 app[web.1]: 1: 0xa08900 node::Abort() [node]
2020-06-27T14:14:53.385189+00:00 app[web.1]: 2: 0xa08d0c node::OnFatalError(char const*, char const*) [node]
2020-06-27T14:14:53.385841+00:00 app[web.1]: 3: 0xb7ef5e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
2020-06-27T14:14:53.386493+00:00 app[web.1]: 4: 0xb7f2d9 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
...
当我在本地运行相同的项目时,它运行良好。当我将“aws-sdk”与常规 JavaScript 应用程序一起使用时,不会发生此错误。我不明白为什么 Heroku 在导入“aws-sdk”时会崩溃?
以下是我的“package.json”:
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts":
"start": "ts-node src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ts-node-dev src/index.ts",
"tsc": "tsc",
"lint": "eslint --ext .ts .",
"deploy": "git push heroku master",
"deploy:full": "git add . && git commit -m deploy && npm run deploy && heroku logs --tail"
,
"author": "",
"license": "ISC",
"devDependencies":
"@typescript-eslint/eslint-plugin": "^3.1.0",
"@typescript-eslint/parser": "^3.1.0",
"eslint": "^7.1.0",
"ts-node-dev": "^1.0.0-pre.49"
,
"dependencies":
"@types/aws-sdk": "^2.7.0",
"@types/cors": "^2.8.6",
"@types/express": "^4.17.6",
"@types/node": "^14.0.10",
"aws-sdk": "^2.699.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"ts-node": "^8.10.2",
"typescript": "^3.9.3"
还有“Procfile”:
web ts-node src/index.ts
【问题讨论】:
【参考方案1】:您是否考虑过仅导入库的子集? 像这样导入所有内容时,我遇到了同样的问题:
import AWS from 'aws-sdk';
内存猛增超过 500mb,高于 Heroku 提供的 512mb(免费套餐)。 AWS SDK 似乎提供代码拆分功能,因此您可以只导入您需要的客户端。在我的例子中:
import S3 from 'aws-sdk/clients/s3';
我的应用程序的内存保持在 200mb 左右,我可以使用该库。
【讨论】:
简单的答案,但仍然有用;我没想到!谢谢 但是,我还没有找到一种方法来使用我的 S3 凭证配置 AWS 开发工具包,而无需导入整个开发工具包。例如here.以上是关于Heroku 在导入“aws-sdk”时崩溃的主要内容,如果未能解决你的问题,请参考以下文章