Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Problem:

I'm trying to build Typescript code which will be deployed to AWS Lambda. However upon Aws Sam build I'm getting error as mentioned in the title. Following is the exact error.

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
}

So I searched and found out that my node_modules didn't have aws-sdk so I install it with

npm install aws-sdk

even I installed

npm install --save-dev @types/node

but somehow esbuild fails, the error mentions to mark the path as external, can someone please guide how to mark it external? I'm sharing my tsconfig.json as below

{
    "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"]
  }
like image 406
Ayub Jamal Avatar asked Feb 28 '26 07:02

Ayub Jamal


1 Answers

Add External: property to Metadata.BuildProperties in "template.yaml"

ex.)

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

like image 95
taka23 Avatar answered Mar 02 '26 19:03

taka23