将已编译的打字稿部署到Lambda时导入模块错误

时间:2020-01-20 19:10:58

标签: node.js typescript aws-lambda

我正在将一些JS代码转换为打字稿,并且在AWS上使用无服务器框架部署到lambda并运行它时,出现以下错误。起初我以为我的进口货很差,但看起来一切都如预期。

{ “ errorType”:“ Runtime.ImportModuleError”, “ errorMessage”:“错误:找不到模块'tslib'\ n需要堆栈:\ n- /var/task/handlers/Create.js\n-> /var/runtime/UserFunction.js\n- / var / runtime / index.js”, “跟踪”:[ “ Runtime.ImportModuleError:错误:找不到模块'tslib'”, “需要堆栈:”, “-/var/task/handlers/Create.js”, “-/var/runtime/UserFunction.js”, “-/var/runtime/index.js”, “位于_loadUserApp(/var/runtime/UserFunction.js:100:13)”, “位于Object.module.exports.load(/var/runtime/UserFunction.js:140:17)”, “在对象。(/var/runtime/index.js:43:30)”, “位于Module._compile(internal / modules / cjs / loader.js:956:30)”, “位于Object.Module._extensions..js(内部/模块/cjs/loader.js:973:10)”, “位于Module.load(internal / modules / cjs / loader.js:812:32)”, “在Function.Module._load(internal / modules / cjs / loader.js:724:14)”, “位于Function.Module.runMain(internal / modules / cjs / loader.js:1025:10)”, “位于internal / main / run_main_module.js:17:11上” ] }

无服务器:

functions:
  createFamily:
    name: ${self:custom.createFamilyName}
    handler: handlers/Create.createFamily
    description: Lambda for Creating a family in the family service
    timeout: 30
    events:
      - http:
          path: /family
          method: post
          private: true

处理程序:

import { APIGatewayEvent, Context, ProxyResult } from 'aws-lambda';
import { utilities } from '../handlers/utilities';
import { familyService } from '../services/FamilyService';
import { IFamily } from '../interfaces/IFamily';

let familyData: IFamily;
let serverReturn: IFamily;

export const createFamily = async (
    event: APIGatewayEvent,
    context: Context
): Promise<ProxyResult> => {
    try {
        if (!event.body) {
            return utilities.BuildResponse(400, JSON.stringify('Object to create was not provided'));
        }
        familyData = JSON.parse(event.body);
        serverReturn = await familyService.createFamily(familyData);

        if (!serverReturn) {
            return utilities.BuildResponse(404, JSON.stringify('Failed to create Family'));
        }

        return utilities.BuildResponse(201, JSON.stringify(serverReturn));
    } catch(err) {
        console.error('Family Service Create a family error: ', err);
        return utilities.BuildResponse(500, JSON.stringify('Family Service internal server error'));
    }
}

1 个答案:

答案 0 :(得分:3)

您需要将打字稿编译为javascript,并在模板中引用转译的输出。