Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

Typescript compilation through Serverless failing

I have a Typescript-based Lambda function that compiles fine with tsc but when I attempt to deploy through Serverless, the Typescript complication fails with the following error:

Serverless: Running "serverless" installed locally (in service node_modules)
Serverless: Compiling with Typescript...
Serverless: Using local tsconfig.json
Serverless: Warning: "rootDir" from local tsconfig.json is overriden
Cannot locate handler - account. not found
 
  Error --------------------------------------------------
 
  Error: Typescript compilation failed. Please ensure handlers exists with ext .ts or .js
      at /Users/bob/Development/service-account/node_modules/serverless-plugin-typescript/src/typescript.ts:69:13

The relevant chunks of account.ts code are:

import { APIGatewayProxyHandler, APIGatewayEvent, Context } from 'aws-lambda';
import 'source-map-support/register';

export const handler: APIGatewayProxyHandler = async (event: APIGatewayEvent, context: Context): Promise<any> => {
    console.debug(`event: ${JSON.stringify(event, null, 1)}`);
    console.debug(`context: ${JSON.stringify(context, null, 1)}`);
...
}

The relevant sections of the serverless.yml file are:

plugins:
  - serverless-iam-roles-per-function
  - serverless-plugin-typescript
  - serverless-webpack

service: accounts

custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

functions:
  account:
    handler: account.handler
...

The tsconfig.json is rather standard:

{
  "compilerOptions": {
    "target": "es2019",
    "module": "commonjs",
    "lib": [
      "es2020",
      "dom"
    ],
    "strict": true,
    "esModuleInterop": true,
    "rootDir": "./src",
    "outDir": "./dist",
  },
  "include": [
    "./src/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

The directory tree looks like this:

config <dir>
dist <dir>
node_modules <dir>
src <dir>
  account.ts
swagger <dir>
package.json
tsconfig.json
webpack.config.js
serverless.yml

Thoughts on what I am missing on connecting all the dots to get this working within Serverless?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to provide the relative path to the handler, this should work

functions:
  account:
    handler: "./src/account.handler"

You may choose to omit ./ and handler: "src/account.handler" should also work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...