aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.05k stars 573 forks source link

util-user-agent-node and signature-v4-multi-region fails with ESM #4373

Closed ciscoheat closed 1 year ago

ciscoheat commented 1 year ago

Checkboxes for prior research

Describe the bug

In version 3.257.0 I suddenly get an ESM-related error when using vitest with a SvelteKit site. By configuring vite to inline the packages, it works again.

SDK version number

@aws-sdk/util-user-agent-node@3.257.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v18.10.0

Reproduction Steps

Sorry but I cannot reproduce this in an easy way. I'm hoping that the error is well-known and has an easy fix.

Observed Behavior

SyntaxError: Cannot use import statement outside a module
 ❯ Object.compileFunction node:vm:360:18

Module .../node_modules/.pnpm/@aws-sdk+util-user-agent-node@3.257.0/node_modules/@aws-sdk/util-user-agent-node/dist-es/is-crt-available.js:1 seems to be an ES Module but shipped in a CommonJS package. You might want to create an issue to the package "@aws-sdk/util-user-agent-node" asking them to ship the file in .mjs extension or add "type": "module" in their package.json.

As a temporary workaround you can try to inline the package by updating your config:

// vitest.config.js
export default {
  test: {
    deps: {
      inline: [
        "@aws-sdk/signature-v4-multi-region"
      ]
    }
  }
}

Expected Behavior

No error message, it used to work fine.

Possible Solution

No response

Additional Information/Context

The suggested workaround works, when adding this to vite.config.js

import { defineConfig } from 'vite';

export default defineConfig(({ command }) => {
  return {
    test: {
      deps: {
        inline: ['@aws-sdk/util-user-agent-node', '@aws-sdk/signature-v4-multi-region']
      }
    }
  }
}
RanVaknin commented 1 year ago

Hi @ciscoheat ,

Thanks for opening this issue.

Since you are using a 3rd party tool, and we don't have repro steps this will make it hard to root cause.

suddenly get an ESM-related error when using vitest with a SvelteKit site. By configuring vite to inline the packages, it works again.

This being a sudden change is worrisome, this might be a breaking change from the SDK side. Can you help us somehow reproduce this error? Perhaps a minimal setup, or an explanation of how you are using the SDK in this case? Are you importing that @aws-sdk/signature-v4-multi-region directly?

Thank you, Ran

ciscoheat commented 1 year ago

Hello Ran, it is difficult to reproduce. By just adding the @aws-sdk library to an empty project gives no problem. And the build works without error with SvelteKit, but when testing it with Vitest, the error occurs, even though they are using the same configuration (both are built on Vite).

I'm not importing any of the mentioned libraries, this is my only reference, in a single file:

import { S3Client, PutObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';

And to make things more confusing, there is no reference to this file in the tests.

danielzting commented 1 year ago

I am getting the same error on @aws-sdk/client-s3 v3.267.0

If you would like to investigate the commit is here. It only popped up when I imported from @aws-sdk/client-s3 in the file under test.

github-actions[bot] commented 12 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.