Home > Net >  Node can't seem to import AWS DynamoDB package into project
Node can't seem to import AWS DynamoDB package into project

Time:12-02

Please note: although I mention AWS SAM and DynamoDB here, I think this is more of an AWS JavaScript SDK, or possibly even just a Node/NPM question at heart, and as such, is probably answerable by anyone with experience writing Node/JavaScript apps on AWS Lambda.


I used AWS SAM to initialize a "Hello World"-style JavaScript Lambda repo, and used it to deploy that Lambda behind an API Gateway. It was actually pretty cool and very little work to get everything configured correctly.

I am now trying to modify the boilerplate TypeScript code (essentially, the TypeScript Lambda handler) to read/write to a DynamoDB table.

In my Lambda handler, I added:

import { PutCommand } from "@aws-sdk/lib-dynamodb";

...and ran sam build, which amongst other things, invokes EsBuild for compiling/linting. This line of code fails with the following error:

Build Failed
Error: NodejsNpmEsbuildBuilder:EsbuildBundle - Esbuild Failed: ✘ [ERROR] Could not resolve "@aws-sdk/lib-dynamodb"

    app.ts:5:27:
      5 │ import { PutCommand } from "@aws-sdk/lib-dynamodb";
        ╵                            ~~~~~~~~~~~~~~~~~~~~~~~

So I look in my package.json (that AWS SAM generated for me), and I see:

{
  "name": "my-lambda",
  "version": "1.0.0",
  "description": "experimental",
  "main": "app.js",
  "repository": "<redacted>",
  "author": "me myself and i",
  "license": "MIT",
  "scripts": {
    "unit": "jest",
    "lint": "eslint '*.ts' --quiet --fix",
    "compile": "tsc",
    "test": "npm run compile && npm run unit"
  },
  "devDependencies": {
    "@types/aws-lambda": "^8.10.92",
    "@types/jest": "^29.2.0",
    "@types/node": "^18.11.4",
    "@typescript-eslint/eslint-plugin": "^5.10.2",
    "@typescript-eslint/parser": "^5.10.2",
    "esbuild": "^0.14.14",
    "esbuild-jest": "^0.5.0",
    "eslint": "^8.8.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "^29.2.1",
    "prettier": "^2.5.1",
    "ts-node": "^10.9.1",
    "typescript": "^4.8.4"
  }
}

So, I think this makes sense, right? The aws-sdk/lib-dynamodb package is not installed. So, from the root of the repo, I run:

npm i @aws-sdk/lib-dynamodb

And then re-run sam build...only to get the exact same error.

Why am I getting this error and what can I do to fix it so that my Lambda handler call pull in all the necessary components of the AWS DynamdoDB JavaScript client?

CodePudding user response:

The name of the package which holds the client is @aws-sdk/client-dynamodb.

Install and import from that.

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-dynamodb/index.html

  • Related