简体   繁体   English

Typescript 模块在通过 cdk 部署时未在 lambda function 中导入

[英]Typescript modules are not been imported in lambda function while its deploying through cdk

I use CDK to develop my serverless application in AWS.我使用 CDK 在 AWS 中开发我的无服务器应用程序。 When I try to deploy a lambda function post after TS file compilation, its not importing TS modules in lambda function as JS modules. When I try to deploy a lambda function post after TS file compilation, its not importing TS modules in lambda function as JS modules.

Due to that, I am facing an Module not found error when I invoke my lambda.因此,当我调用 lambda 时,我遇到了未找到模块的错误。

The steps that I follow before I deploy the stack:我在部署堆栈之前遵循的步骤:

  1. tsc -> to complile TS files tsc -> 编译 TS 文件
  2. cdk synth cdk合成器
  3. cdk deploy cdk 部署

Ts config: ts配置:

{
  "compilerOptions": {
    "target": "ES2018",
    "module": "commonjs",
    "lib": [
      "es2018"
    ],
    "noEmit": false,
    "declaration": false,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "typeRoots": [
      "./node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "cdk.out"
  ]
}

Code:代码:

import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';
import { DynamoDBClient, GetItemCommand, GetItemCommandInput } from "@aws-sdk/client-dynamodb";
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";

export async function handler(event: APIGatewayProxyEventV2,): Promise<APIGatewayProxyResultV2> {
console.log('event', event);

let userLat = event.pathParameters?.lat;
let userLng = event.pathParameters?.lng;    
let tableName = process.env.TABLE_NAME;
let results;

const dbClient: DynamoDBClient = new DynamoDBClient({ region: "ap-south-1" });
let params: GetItemCommandInput;

if (tableName) {
     params = {
        TableName: tableName,
        Key: marshall({
          "lat": userLat, 
          "lng": userLng
        }),
      };
}

const run = async function () {
    try {
        const resp = await dbClient.send(new GetItemCommand(params));
        results = unmarshall(resp.Item || {});
    } catch(err) {
        results = err;
    }
};

run();

return {
    body: JSON.stringify(
        results
    ),
    statusCode: 200
};     }

Details:细节:

  • Node: v14.20.0节点:v14.20.0
  • NPM: 6.14.17 NPM:6.14.17
  • CDK: 2.40.0 (build 56ba2ab) CDK:2.40.0(构建 56ba2ab)
  • Ts: Version 4.8.2 Ts:版本 4.8.2

Error:错误:

{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module '@aws-sdk/client-dynamodb'\nRequire stack:\n- /var/task/fetchpartner.js\n- /var/runtime/UserFunction.js\n- /var/runtime/Runtime.js\n- /var/runtime/index.js", "trace": [ "Runtime.ImportModuleError: Error: Cannot find module '@aws-sdk/client-dynamodb'", "Require stack:", "- /var/task/fetchpartner.js", "- /var/runtime/UserFunction.js", "- /var/runtime/Runtime.js", "- /var/runtime/index.js", " at _loadUserApp (/var/runtime/UserFunction.js:221:13)", " at Object.module.exports.load (/var/runtime/UserFunction.js:279:17)", " at Object. (/var/runtime/index.js:43:34)", " at Module._compile (internal/modules/cjs/loader.js:1085:14)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)", " at Module.load (internal/modules/cjs/loader.js:950:32)", " at Function.Module._load (internal { "errorType": "Runtime.ImportModuleError", "errorMessage": "错误:找不到模块 '@aws-sdk/client-dynamodb'\n需要堆栈:\n- /var/task/fetchpartner.js\n- / var/runtime/UserFunction.js\n- /var/runtime/Runtime.js\n- /var/runtime/index.js", "trace": [ "Runtime.ImportModuleError: Error: 找不到模块'@aws- sdk/client-dynamodb'", "需要堆栈:", "- /var/task/fetchpartner.js", "- /var/runtime/UserFunction.js", "- /var/runtime/Runtime.js", “- /var/runtime/index.js”、“在 _loadUserApp (/var/runtime/UserFunction.js:221:13)”、“在 Object.module.exports.load (/var/runtime/UserFunction.js: 279:17)”, “在 Object. (/var/runtime/index.js:43:34)”, “在 Module._compile (internal/modules/cjs/loader.js:1085:14)”, “在Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)", "在 Module.load (internal/modules/cjs/loader.js:950:32)", "在 Z86408593C34AF77FDD16Z0DF932F8B52 .Module._load(内部/modules/cjs/loader.js:790:12)", " at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)", " at internal/main/run_main_module.js:17:47" ] } /modules/cjs/loader.js:790:12)", "在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)", "在 internal/main/run_main_module.js:17 :47"]}

You can run npm init in the directory that contains your lambda code and install the modules you want there.您可以在包含 lambda 代码的目录中运行npm init并在那里安装您想要的模块。 Another way is to use something like webpack to compile and combine your code and modules into a single file.另一种方法是使用 webpack 之类的东西来编译并将您的代码和模块组合到一个文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模块可以访问未导入的其他模块中存储的变量,但两个模块都导出到主文件吗? - Can Modules access variables stored in other modules that have not been imported, but both modules are exported to a main file? 如何将带有导入模块的 typescript 转换为浏览器友好的 js 文件 - How to transpile typescript with imported modules to a browser friendly js-file 仅在结果不是对象/数组的情况下,才能对导入的模块进行打字稿单元测试 - Typescript Unit Tests on imported modules only works if result is not object/array Typescript找不到使用Webpack别名导入的模块 - Typescript can't find modules which are imported with webpack alias Typescript项目-如何将导入的模块合并到最终版本中? - Typescript project - how to incorporate imported modules into final build? Node.js 和 Typescript,如何动态访问导入的模块 - Node.js and Typescript, how to dynamically access imported modules 将 aws cdk 与 typescript 结合使用时出现许多构建错误 - Many build errors while using aws cdk in combination with typescript 为什么此lambda函数不是打字稿中的错误? - Why is this lambda function not an error in typescript? 用 Jest 模拟 Typescript 中导入的 function 的返回值 - Mock the return value of an imported function in Typescript with Jest 尝试使用导入的 js 函数启动 html 页面时,“CORS 策略已阻止从源‘null’访问脚本”错误 - "access to script from origin 'null' has been blocked by CORS policy" error while trying to launch an html page using an imported js function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM