Hey,
I have a large application build with NestJS
that I deploy using the serverless framework
. I have been doing this for some time and everything has been great. A couple of days ago I had to update to Nestjs 7
and I have been experiencing a lot of issues bootstrapping my application when it is deployed to aws
. After going through countless frustrating attempts to resolve the issue it appears it’s actually nothing to do with the Nestjs/serverless bootstrapping process at all and apollo-server-express
was unable to access the express router - failing with the error:
express_1.default.Router is not a function
Finally I realised that when I import express
directly and try and access express.Router()
I have the same issue. So I made a very simple test:
lambda.ts:
import { Context, Handler } from "aws-lambda";
import express from "express";
export const handler: Handler = async (event: any, context: Context) => {
console.log("Import express:", express);
console.log("Test express app: ", express());
console.log("Test router:", express.Router());
/* express.Router() ->
ERROR TypeError: express_1.default.Router is not a function at
/var/task/dist/lambda.js:19:51 at Generator.next (<anonymous>) at
/var/task/dist/lambda.js:8:71 at new Promise (<anonymous>) at
__awaiter (/var/task/dist/lambda.js:4:12) at exports.handler (/var/task/dist/lambda.js:16:39) at
Runtime.handler (/var/task/serverless_sdk/index.js:9:131872) at
Runtime.handleOnce (/var/runtime/Runtime.js:66:25)
*/
};
This fails with the error in the comment as previously stated. Here are the other files:
serverless.yml:
service: xxxxx
app: xxxx
tenant: xxxxx
plugins:
- serverless-pseudo-parameters
- serverless-prune-plugin
- serverless-deployment-bucket
provider:
name: aws
runtime: nodejs12.x
region: eu-west-1
stage: dev
timeout: 29
memorySize: 3008
deploymentBucket:
name: ${self:service}-${self:custom.currentStage}-deployment-bucket
serverSideEncryption: AES256
custom: ${file(./serverless-common.yml):custom}
package:
include:
- ./dist/**
exclude:
- node_modules/aws-sdk/**
functions:
index:
handler: ./dist/lambda.handler
name: bm-${self:custom.currentStage}-express-test
events:
- http:
path: "/{proxy+}"
method: POST
package.json:
{
"name": "@xxx/XXXXXX",
"version": "0.1.13",
"dependencies": {
"express": "4.17.1"
},
"devDependencies": {
"serverless-deployment-bucket": "1.1.1",
"serverless-prune-plugin": "1.4.2",
"serverless-pseudo-parameters": "2.5.0",
"ts-node": "^8.7.0",
"tsconfig-paths": "^3.7.0",
"tslint": "5.12.1",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.8.3"
}
}
tsconfig.json:
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@root/*": ["src/*"]
},
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "dist",
"esModuleInterop": true
},
"include": ["*"],
"exclude": ["**/node_modules/**/*", "dist"]
}
I have been working on this problem for ages before narrowing it down to this. If anybody is able to shed any light on the above that would be greatly appreciated as it’s obviously blocking me.
I would like to highlight that this code only fails once deployed to lambda. It runs fine locally which would make indicate that perhaps something was up with the packaging process but the zip file contains the correct code and dependencies.
Many thanks,
Mark