【问题标题】:Typescript compilation through Serverless failing通过无服务器失败的 Typescript 编译
【发布时间】:2021-04-16 00:41:27
【问题描述】:

我有一个基于 Typescript 的 Lambda 函数,可以使用 tsc 很好地编译,但是当我尝试通过 Serverless 进行部署时,Typescript 复杂性失败并出现以下错误:

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

account.ts 代码的相关块是:

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)}`);
...
}

serverless.yml 文件的相关部分是:

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

tsconfig.json 相当标准:

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

目录树如下所示:

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

关于连接所有点以使其在无服务器中工作时我缺少什么的想法?

【问题讨论】:

  • 你能从你的项目根目录及下面发布目录树吗?

标签: typescript serverless-framework


【解决方案1】:

您需要提供处理程序的相对路径,这应该可以工作

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

您可以选择省略./handler: "src/account.handler" 也应该可以工作。

【讨论】:

  • 谢谢你,这不是我能想到的。我会相信 rootDir 会解决这个问题的。我已经转向以下错误:无服务器错误 ---------------------------------------- webpack 插件在以下位置找不到配置文件:/Users/bob/Development/service-account/.build/webpack.config.js
  • 您的问题中的文件列表丢失webpack.config.js,通常它是在您使用命令设置新项目时创建的:sls create --template aws-nodejs-typescript
  • 它在那里,我只是把它命名错了。我认为它与:github.com/prisma-labs/serverless-plugin-typescript/issues/219
【解决方案2】:

修改文件tsconfig.json

改变

"include": [
    "./src/*.ts"
]

"include": [
    "./src/**/*.ts"
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多