aws-sdk-js-v3 icon indicating copy to clipboard operation
aws-sdk-js-v3 copied to clipboard

publish Node.js module compatible ESM (.mjs) artifacts

Open JamesKyburz opened this issue 2 years ago • 33 comments

Describe the bug

The dist-es code published to NPM is never run when no bundler is used.

Your environment

SDK version number

@aws-sdk/<all I think>@3.94.0

Is the issue in the browser/Node.js/ReactNative?

No

Details of the browser/Node.js/ReactNative version

Paste output of npx envinfo --browsers or node -v or react-native -v

LTS version of node Now using node v16.15.0 (npm v8.5.5)

Steps to reproduce

cd /tmp
mkdir -p aws-sdk-test
cd aws-sdk-test
echo '{"type": "module"}' > package.json
npm i @aws-sdk/client-dynamodb
echo -e '\n\nconsole.log("ESM!")' >> node_modules/@aws-sdk/client-dynamodb/dist-es/index.js
echo -e '\n\nconsole.log("CJS!")' >> node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js
echo "import { DynamoDBClient } from '@aws-sdk/client-dynamodb'" > index.js
node index.js # CJS!

Observed behavior

The code shipped in dist-es doesn't work in Node either because it's non standard (for example file extensions are missing).

Adding the exports field to package.json + index.mjs would work if the dist-es code was compatible with node.

  "exports": {
    "import": "./dist-es/index.mjs",
    "require": "./dist-cjs/index.js"
  }

Expected behavior

Using this module with vanilla node runs the ESM code.

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

Add any other context about the problem here.

JamesKyburz avatar May 19 '22 12:05 JamesKyburz

HI @JamesKyburz thanks for bringing this up, one of the many solutions is indeed to rename the files, the team is already aware of this and working on a solution. Will keep you posted.

ajredniwja avatar May 27 '22 16:05 ajredniwja

@ajredniwja Thanks, I have quite a long exclude list when deploying lambdas the keep the size down :)

Some of them are

- '!./**/node_modules/typescript'
- '!./**/node_modules/@types'
- '!./**/node_modules/@aws-sdk/**/dist-es'
- '!./**/node_modules/@aws-sdk/**/dist-types'
- '!./**/CHANGELOG.md'
- '!./**/LICENSE*'
- '!./**/README.md'

JamesKyburz avatar May 28 '22 14:05 JamesKyburz

Even without the file name extensions the use of the "module" field I believe is non-standard and not respected by anything I'm using

The exports field should be added

thdxr avatar Jun 01 '22 23:06 thdxr

Can we get an update on this? The follow very basic example fails despite aws docs mentioning ESM in several places

import { S3Client, ListBucketsCommand } from "@aws-sdk/client-s3";

const client = new S3Client({});
const result = await client.send(new ListBucketsCommand());
console.log(result);

thdxr avatar Jul 06 '22 12:07 thdxr

I'm also interested in when this can be rectified

rcoundon avatar Jul 13 '22 06:07 rcoundon

@ajredniwja can we get some kind of update?

thdxr avatar Jul 16 '22 16:07 thdxr

bump

aisflat439 avatar Jul 26 '22 13:07 aisflat439

It would be helpful for us as well to get an update/resolution on this soon; thanks!

blytheaw avatar Jul 26 '22 14:07 blytheaw

Ditto, a response would be really appreciated. Thank you!

steviec avatar Jul 26 '22 14:07 steviec

same

kdallen1 avatar Jul 26 '22 17:07 kdallen1

The community is eager for a fix

revmischa avatar Jul 26 '22 19:07 revmischa

Can the community do something to help fix this? What are the next steps?

adsc avatar Jul 26 '22 19:07 adsc

Also interested, thanks!

kevinggrimm avatar Jul 26 '22 23:07 kevinggrimm

I'm also interested in getting an eta for this one!

lkow avatar Jul 27 '22 07:07 lkow

Please, I need this fix too.

acamaral avatar Jul 27 '22 16:07 acamaral

+1 hope this gets fixed asap

ppold avatar Jul 28 '22 13:07 ppold

A good start would be making sure all relative imports use file extensions .js, which is required by vanilla esm, and works in typescript too.

JamesKyburz avatar Aug 20 '22 12:08 JamesKyburz

Please build and host your esm dist. I can recommend a CDN service called CloudFront.

Then dog food it, no build tools especially with flagship clients, CognitoProvider and even Amplify.configure(), no reason node should ever be required to run any getting started guide. Check out the other guys for inspiration.

<javascript type="module">
  import { initializeApp } from "https://www.gstatic.com/firebasejs/9.9.3/firebase-app.js";
  const firebaseConfig = {}
  const app = initializeApp(firebaseConfig);
</javascript>

rromanchuk avatar Sep 29 '22 22:09 rromanchuk

Oh wow they are actually resolving now, at least the ones i just tried, haven't tried again with node build tools.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Untitled</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <!--
    JSPM Generator Import Map
    Edit URL: https://generator.jspm.io/#U2VhYGDhD80rySzJSU1hcEgsL9YtTsnWT87JTM0r0U3OT8/LLMnXzUxJBSmp1C0oyi8DcoocjPUMLQz0DDB0ZGfmpRZnFuuCVOXrFmem5yXmZOalE6cep6piY5gUAOgTvPexAA
  -->
  <script type="importmap">
  {
    "imports": {
      "@aws-sdk/client-cognito-identity-provider": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
      "@aws-sdk/client-kinesis-video": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
      "@aws-sdk/client-kinesis-video-signaling": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
      "@aws-sdk/client-s3": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js"
    },
    "scopes": {
      "https://ga.jspm.io/": {
        "@aws-crypto/crc32": "https://ga.jspm.io/npm:@aws-crypto/[email protected]/build/index.js",
        "@aws-crypto/crc32c": "https://ga.jspm.io/npm:@aws-crypto/[email protected]/build/index.js",
        "@aws-crypto/util": "https://ga.jspm.io/npm:@aws-crypto/[email protected]/build/index.js",
        "@aws-sdk/client-sso": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/client-sts": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/config-resolver": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/credential-provider-env": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/credential-provider-imds": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/credential-provider-ini": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/credential-provider-node": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/credential-provider-process": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/credential-provider-sso": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/credential-provider-web-identity": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/eventstream-codec": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/eventstream-serde-config-resolver": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/eventstream-serde-node": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/eventstream-serde-universal": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/hash-node": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/hash-stream-node": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/is-array-buffer": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-bucket-endpoint": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-content-length": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-expect-continue": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-flexible-checksums": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-host-header": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-location-constraint": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-logger": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-recursion-detection": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-retry": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-sdk-s3": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-sdk-sts": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-serde": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-signing": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-ssec": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-stack": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/middleware-user-agent": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/node-config-provider": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/node-http-handler": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/property-provider": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/protocol-http": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/querystring-builder": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/querystring-parser": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/service-error-classification": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/shared-ini-file-loader": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/signature-v4": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/signature-v4-multi-region": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/smithy-client": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/url-parser": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-arn-parser": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-base64-node": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-body-length-node": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-buffer-from": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-config-provider": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-defaults-mode-node": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-hex-encoding": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-middleware": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-stream-node": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-uri-escape": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-user-agent-node": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-utf8-browser": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-utf8-node": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/util-waiter": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "@aws-sdk/xml-builder": "https://ga.jspm.io/npm:@aws-sdk/[email protected]/dist-es/index.js",
        "buffer": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/buffer.js",
        "child_process": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/child_process.js",
        "crypto": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/crypto.js",
        "entities": "https://ga.jspm.io/npm:[email protected]/lib/dev.index.js",
        "fast-xml-parser": "https://ga.jspm.io/npm:[email protected]/src/parser.js",
        "fs": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/fs.js",
        "http": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/http.js",
        "http2": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/http2.js",
        "https": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/https.js",
        "os": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/os.js",
        "path": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/path.js",
        "process": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/process.js",
        "stream": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/stream.js",
        "tslib": "https://ga.jspm.io/npm:[email protected]/tslib.es6.js",
        "url": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/url.js",
        "util": "https://ga.jspm.io/npm:@jspm/[email protected]/nodelibs/browser/util.js",
        "uuid": "https://ga.jspm.io/npm:[email protected]/dist/esm-browser/index.js"
      },
      "https://ga.jspm.io/npm:@aws-crypto/[email protected]/": {
        "tslib": "https://ga.jspm.io/npm:[email protected]/tslib.es6.js"
      },
      "https://ga.jspm.io/npm:@aws-crypto/[email protected]/": {
        "tslib": "https://ga.jspm.io/npm:[email protected]/tslib.es6.js"
      }
    }
  }
  </script>
  
  <!-- ES Module Shims: Import maps polyfill for modules browsers without import maps support (all except Chrome 89+) -->
  <script async src="https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js" crossorigin="anonymous"></script>
  
  <script type="module">
    import * as SdkClientCognitoIdentityProvider from "@aws-sdk/client-cognito-identity-provider";
    import * as SdkClientKinesisVideo from "@aws-sdk/client-kinesis-video";
    import * as SdkClientKinesisVideoSignaling from "@aws-sdk/client-kinesis-video-signaling";
    import * as SdkClientS3 from "@aws-sdk/client-s3";
  
    // Write main module code here, or as a separate file with a "src" attribute on the module script.
    console.log(SdkClientCognitoIdentityProvider, SdkClientKinesisVideo, SdkClientKinesisVideoSignaling, SdkClientS3);
  </script>
</body>
</html>

rromanchuk avatar Sep 29 '22 22:09 rromanchuk

bump

jordiup avatar Aug 22 '23 08:08 jordiup

Are there any plans to fix this? The latest versions have the same issues as mentioned when creating this issue.

JamesKyburz avatar Sep 21 '23 13:09 JamesKyburz

One way forward would be to

  • use moduleResolution node16 in tsconfig.json files to prevent invalid relative imports
  • add the exports (imports and require) to each package.json documented here

JamesKyburz avatar Sep 27 '23 16:09 JamesKyburz

Any news on this as Lambda Node 20 is probably coming soon?

JamesKyburz avatar Nov 09 '23 11:11 JamesKyburz

jest would probably have to be fixed too for this to be pushed forward?

https://github.com/jestjs/jest/issues/13350

JamesKyburz avatar Dec 06 '23 09:12 JamesKyburz

Any news?

JamesKyburz avatar Jan 20 '24 16:01 JamesKyburz

bump!!!

cyrfer avatar Feb 27 '24 00:02 cyrfer

Any news?

tgdn avatar Mar 06 '24 07:03 tgdn

Any news?

JamesKyburz avatar Apr 05 '24 22:04 JamesKyburz