简体   繁体   中英

Heroku crashes when importing 'aws-sdk'

I have typescript node.js app, where I use Amazon S3. It's deployed to 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}`);
});

Problem is that if I uncomment import aws from 'aws-sdk' Heroku crashes with the following errors:

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]
...

When I run same project locally, it works fine. This error does not occur when I use 'aws-sdk' with regular JavaScript app. I can't figure out why Heroku crashes when importing 'aws-sdk'?

Below are my '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"
  }
}

And 'Procfile':

web ts-node src/index.ts

Have you considered importing only the subset of the library? I've run in to the same issue when importing everything like so:

import AWS from 'aws-sdk';

The memory shoots up over 500mb which is above the 512mb offered (free tier) from Heroku. AWS SDK seems to offer code splitting so you may import only the client that you need. In my example:

import S3 from 'aws-sdk/clients/s3';

The memory of my application stayed around 200mb and I was able to utilise the library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM