typescript 错误:节点jsNpmEsbuildBuilder:EsbuildBundle - Esbuild失败:X [错误]无法解析“aws-sdk”

dtcbnfnu  于 2022-11-30  发布在  TypeScript
关注(0)|答案(1)|浏览(200)

问题:

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

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
Running NodejsNpmEsbuildBuilder:CopySource
Running NodejsNpmEsbuildBuilder:NpmInstall
Running NodejsNpmEsbuildBuilder:EsbuildBundle

Build Failed
Error: NodejsNpmEsbuildBuilder:EsbuildBundle - Esbuild Failed: X [ERROR] Could not resolve "aws-sdk"

    app.ts:2:21:
      2 │ import * as AWS from 'aws-sdk';
        ╵                      ~~~~~~~~~

  You can mark the path "aws-sdk" as external to exclude it from the bundle, which will remove this error.

1 error
child_process.js:830
    throw err;
    ^

Error: 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
    at checkExecSyncError (child_process.js:790:11)
    at Object.execFileSync (child_process.js:827:15)
    at Object.<anonymous> (C:\Users\AyubJamal\AppData\Roaming\npm\node_modules\esbuild\bin\esbuild:209:28)
    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/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  status: 1,
  signal: null,
  output: [ null, null, null ],
  pid: 23712,
  stdout: null,
  stderr: null
}

我搜索了一下,发现我的node_modules没有aws-sdk,所以我用

npm install aws-sdk

即使我安装了

npm install --save-dev @types/node

但不知何故esbuild失败了,错误提到要将路径标记为外部,有人能指导如何将其标记为外部吗?

{
    "compilerOptions": {
      "target": "es2020",
      "strict": true,
      "preserveConstEnums": true,
      "noEmit": true,
      "sourceMap": false,
      "module":"es2015",
      "moduleResolution":"node",
      "esModuleInterop": true, 
      "skipLibCheck": true,
      "forceConsistentCasingInFileNames": true,  
    },
    "exclude": ["node_modules", "**/*.test.ts"]
  }
mum43rcc

mum43rcc1#

将“Externl:“添加到元数据。在“template.yaml”中构建属性
例如)

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"

Metadata properties in AWS Documents

相关问题