错误:NodejsNpmEsbuildBuilder:EsbuildBundle - Esbuild 失败:X [错误] 无法解析“aws-sdk”

Ayu*_*mal 2 node.js typescript aws-lambda aws-sam

问题:

\n

我正在尝试构建将部署到 AWS Lambda 的 Typescript 代码。然而,在 Aws Sam 构建时,我遇到了标题中提到的错误。以下是确切的错误。

\n
Building codeuri: D:\\AWS\\demo-app\\hello-world runtime: nodejs16.x metadata: {'BuildMethod': 'esbuild', 'BuildProperties': {'Minify': True, 'Target': 'es2020', 'EntryPoints': ['app.ts']}} architecture: x86_64 functions: HelloWorldFunction\nRunning NodejsNpmEsbuildBuilder:CopySource\nRunning NodejsNpmEsbuildBuilder:NpmInstall\nRunning NodejsNpmEsbuildBuilder:EsbuildBundle\n\nBuild Failed\nError: NodejsNpmEsbuildBuilder:EsbuildBundle - Esbuild Failed: X [ERROR] Could not resolve "aws-sdk"\n\n    app.ts:2:21:\n      2 \xe2\x94\x82 import * as AWS from 'aws-sdk';\n        \xe2\x95\xb5                      ~~~~~~~~~\n\n  You can mark the path "aws-sdk" as external to exclude it from the bundle, which will remove this error.\n\n1 error\nchild_process.js:830\n    throw err;\n    ^\n\nError: Command failed: C:\\Users\\AyubJamal\\AppData\\Roaming\\npm\\node_modules\\esbuild\\node_modules\\esbuild-windows-64\\esbuild.exe app.ts --bundle --platform=node --outdir=D:\\AWS\\demo-app\\.aws-sam\\build\\HelloWorldFunction --format=cjs --minify --target=es2020\n    at checkExecSyncError (child_process.js:790:11)\n    at Object.execFileSync (child_process.js:827:15)\n    at Object.<anonymous> (C:\\Users\\AyubJamal\\AppData\\Roaming\\npm\\node_modules\\esbuild\\bin\\esbuild:209:28)\n    at Module._compile (internal/modules/cjs/loader.js:1085:14)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)\n    at Module.load (internal/modules/cjs/loader.js:950:32)\n    at Function.Module._load (internal/modules/cjs/loader.js:790:12)\n    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)\n    at internal/main/run_main_module.js:17:47 {\n  status: 1,\n  signal: null,\n  output: [ null, null, null ],\n  pid: 23712,\n  stdout: null,\n  stderr: null\n}\n
Run Code Online (Sandbox Code Playgroud)\n

于是我搜索了一下,发现我的node_modules没有aws-sdk,所以我安装它

\n
npm install aws-sdk\n
Run Code Online (Sandbox Code Playgroud)\n

即使我安装了

\n
npm install --save-dev @types/node\n
Run Code Online (Sandbox Code Playgroud)\n

但不知何故 esbuild 失败了,错误提到将路径标记为外部,有人可以指导如何将其标记为外部吗?我正在分享我的 tsconfig.json 如下

\n
{\n    "compilerOptions": {\n      "target": "es2020",\n      "strict": true,\n      "preserveConstEnums": true,\n      "noEmit": true,\n      "sourceMap": false,\n      "module":"es2015",\n      "moduleResolution":"node",\n      "esModuleInterop": true, \n      "skipLibCheck": true,\n      "forceConsistentCasingInFileNames": true,  \n    },\n    "exclude": ["node_modules", "**/*.test.ts"]\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

小智 7

将属性添加External:到“template.yaml”中的 Metadata.BuildProperties

前任。)

Metadata: # Manage esbuild properties
  BuildMethod: esbuild
  BuildProperties:
    Minify: true
    Target: "es2020"
    Sourcemap: true
    EntryPoints: 
      - app.ts
    # ==== add below ====
    External:
      - "@aws-sdk/lib-dynamodb"
      - "@aws-sdk/client-dynamodb"
Run Code Online (Sandbox Code Playgroud)

AWS Documents 中的元数据属性