Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module '@aws-sdk/client-dynamodb' with v2 sdk #196

Open
miyamonz opened this issue Dec 13, 2022 · 4 comments
Open

Cannot find module '@aws-sdk/client-dynamodb' with v2 sdk #196

miyamonz opened this issue Dec 13, 2022 · 4 comments

Comments

@miyamonz
Copy link

Describe the bug

use electrodb in a project uses dynamodb v2 sdk, it shows the error.

Error: Cannot find module '@aws-sdk/client-dynamodb'
Require stack:
- /Users/miyamonz/my-project/node_modules/@aws-sdk/lib-dynamodb/dist-cjs/commands/BatchExecuteStatementCommand.js
- /Users/miyamonz/my-project/node_modules/@aws-sdk/lib-dynamodb/dist-cjs/DynamoDBDocument.js
- /Users/miyamonz/my-project/node_modules/@aws-sdk/lib-dynamodb/dist-cjs/index.js
- /Users/miyamonz/my-project/node_modules/electrodb/src/client.js
- /Users/miyamonz/my-project/node_modules/electrodb/src/entity.js
- /Users/miyamonz/my-project/node_modules/electrodb/index.js

ElectroDB Version
2.3.3

Entity/Service Definitions

It seems it's not related to this issue, but just in case.

const handPose = new Entity(
  {
    model: {
      entity: "handPose",
      version: "1",
      service: "hand",
    },
    attributes: {
      poseId: {
        type: "string",
        required: true,
        readOnly: true,
      },
      createdAt: {
        type: "string",
        required: true,
        readOnly: true,
        default: () => new Date().toISOString(),
      },
      updatedAt: {
        type: "string",
        required: true,
        default: () => new Date().toISOString(),
      },
      data: {
        type: CustomAttributeType<HandPoseData>("any"),
        required: true,
      },
    },
    indexes: {
      poses: {
        pk: {
          field: "pk",
          composite: [],
          template: `hand#pose`,
        },
        sk: {
          field: "gsipk1",
          composite: ["poseId"],
          template: "poseId#${poseId}",
        },
      },
    },
  },
  { table: TABLE_NAME, client }
);

Expected behavior

it should work with v2 client without installing v3 client.

BTW, installing v3 though I don't use it in my project that uses v2, then the error disappeared, of course,
but importing v2 and v3 causes a type error, so It would be helpful if you could address this issue.

Additional context

const lib = require('@aws-sdk/lib-dynamodb')

I think this line is the problem. it imports v3 client. it should be imported dynamically.

@miyamonz
Copy link
Author

Later I found this https://github.com/tywalch/electrodb/blob/master/buildbrowser.sh
you already did something about this issue.

I don't know how you deploy this as the npm library, but did something go wrong when building or deploying time?
Anyway, In the node_modules/electrodb of my project, the line exists here.
image

@tywalch
Copy link
Owner

tywalch commented Dec 16, 2022

I'll explore ways to bring this in conditionally. That said, you mentioned you have issues with both installed? Can you explain your issue there a bit more?

@kevinlonigro
Copy link

I have only started exploring ElectroDB and encountered the same issue.

Resolved it by only installing the v3 SDK and passed a new instance of DynamoDBClient as a replacement for DocumentClient without issue.

"dependencies": {
"@aws-sdk/client-dynamodb": "^3.245.0",
"electrodb": "^2.3.5"
}

const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
const client = new DynamoDBClient({
region: "us-east-1"
});

...
}, { client, table });

@miyamonz
Copy link
Author

While preparing to reproduce the environment, I found what causes this.

When using npm, there is no problem, but when yarn, it happens.

If you install electrodb by npm, @aws-sdk/client-dynamodb is included by its dependency.

$ npm ls @aws-sdk/client-dynamodb
electrodb-practice@1.0.0 /Users/miyamonz/ghq/github.com/miyamonz/electrodb-practice
└─┬ electrodb@2.3.5
  └─┬ @aws-sdk/lib-dynamodb@3.238.0
    └── @aws-sdk/client-dynamodb@3.238.0

When yarn, yarn add electrodb , then yarn list @aws-sdk/client-dynamodb , nothing exists.
That's why Error: Cannot find module '@aws-sdk/client-dynamodb' happens.

You can easily reproduce this by this:

  • install aws-sdk, electrodb with yarn
  • write some js file that has new Entity
  • run this code

I don't know well about these differences related to dependencies. I'll check this later and add a comment if I find it.


I think it's better to make this library use the AWS SDK that library users have installed themselves.
If you think so, I would like you to consider using peerDependency.

If you want to ignore that method for now and run yarn as well, you might want to add @aws-sdk/client-dynamodb in the dependencies. It's an explicit dependency in this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants