aws-sdk-js-v3: Clients are broken in Turbopack

Checkboxes for prior research

Describe the bug

The AWS SDK packages to broken in Turbopack, complaining node-specific artifacts cannot be resolved. This is not a strict bug report since Turbopack hasn’t been stablized yet. However, it’s the minor issues in the AWS SDK blocking the Turbopack.

I can implement the fix if I have bendwidth. I documented the detailed changes needed below, feel free to pick them up.

SDK version number

@aws-sdk/client-*@all

Which JavaScript Runtime is this issue in?

Browser

Details of the browser/Node.js/ReactNative version

N/A

Reproduction Steps

Checkout the with-s3-client branch of the Next 13 test repo, and start dev server with yarn dev.

Observed Behavior

FS related module cannot be resolved error.

Expected Behavior

The bundling should not have problems

Possible Solution

Because of the lack of the documentation I spent quite a lot of time in the Turbopack bundling and found the 3 fixed below is needed for the code to work in Turbopack for client-side rendering:

  1. Add extensions to browser field: Latest version of AWS SDK browser field looks like:

    "browser": {
        "./dist-es/runtimeConfig": "./dist-es/runtimeConfig.browser"
      },
    

    It works well with all the other bundlers, but Turbopack is more strict. It requires .js extension for both key and value.

  2. The package @aws-sdk/util-user-agent-node specifies optional dependency “aws-crt”. The dependency is, even though only exists in Node.js specific code path, still loaded and analyzed by Turbopack and hence causing “Unable to resolve package” error. This particular issue may be a Turbopack issue, but I find it can be worked around with a fix on AWS SDK side — using createRequire in the dist-es artifact to require optional dependencies instead of calling require() directly. The fix on the AWS SDK side is more “correct” as the current implementation does miss some considerations.

  3. The package @aws-sdk/signature-v4-multi-region packaga contains require() api to assert the optional Node.js specific dependency @aws-sdk/signature-v4-crt (link). The being in the browser code path is causing errors in the Turbopack. This code can be moved to a separate file and disabled in Node.js runtime:

// loadSigv4aSignerCtor.js
export const loadSignv4aSignerCtr = typeof require === "function" && require("@aws-sdk/signature-v4-crt").CrtSignerV4;

// SignatureV4MultiRegion.js
getSigv4aSigner() {
        if (!this.sigv4aSigner) {
            let CrtSignerV4;
            try {
                CrtSignerV4 = loadSignv4aSignerCtr(); // <==== here's the change
                if (typeof CrtSignerV4 !== "function")
                    throw new Error();
            }
            catch (e) {
                e.message =
                    `${e.message}\nPlease check if you have installed "@aws-sdk/signature-v4-crt" package explicitly. \n` +
                        "For more information please go to " +
                        "https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt";
                throw e;
            }
            this.sigv4aSigner = new CrtSignerV4({
                ...this.signerOptions,
                signingAlgorithm: 1,
            });
        }
        return this.sigv4aSigner;
    }

//package.json
"browser": {
    "./dist-es/loadSigv4aSignerCtor.js": false,
    "@aws-sdk/signature-v4-crt": false
  },

Additional Information/Context

No response

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 11
  • Comments: 17 (3 by maintainers)

Most upvoted comments

Hi, any update on this? I get this problem with NextJs 13 when using webpack. The only difference is that I have app directory enabled (https://github.com/aws-amplify/amplify-js/issues/11030). It makes working with the project problematic as it slows down bundling a lot.

Module not found: Can't resolve 'aws-crt' in '/Users/tomekm/Desktop/tixy-frontend/node_modules/@aws-sdk/client-lex-runtime-service/node_modules/@aws-sdk/util-user-agent-node/dist-cjs'

Import trace for requested module:
./node_modules/@aws-sdk/client-lex-runtime-service/node_modules/@aws-sdk/util-user-agent-node/dist-cjs/is-crt-available.js
./node_modules/@aws-sdk/client-lex-runtime-service/node_modules/@aws-sdk/util-user-agent-node/dist-cjs/index.js
./node_modules/@aws-sdk/client-lex-runtime-service/dist-cjs/runtimeConfig.js
./node_modules/@aws-sdk/client-lex-runtime-service/dist-cjs/LexRuntimeServiceClient.js
./node_modules/@aws-sdk/client-lex-runtime-service/dist-cjs/index.js
./node_modules/@aws-amplify/interactions/lib/Providers/AWSLexProvider.js
./node_modules/@aws-amplify/interactions/lib/index.js
./node_modules/aws-amplify/lib/index.js
./src/graphql/amplifyConfigure.ts

./node_modules/@aws-sdk/client-lex-runtime-v2/node_modules/@aws-sdk/util-user-agent-node/dist-cjs/is-crt-available.js
Module not found: Can't resolve 'aws-crt' in '/Users/

Any news on that? This is blocking a lot of NextJS 13 users. Thanks! 😃

Is there an ETA on the fix? As of NextJs 13.4, the app directory was marked stable. This error is inhibiting development with a popular and widely used framework.

Like @AllanZhengYP mentioned:

This is not a strict bug report since Turbopack hasn’t been stablized yet. However, it’s the minor issues in the AWS SDK blocking the Turbopack.

We will look into it when Turbopack becomes stable.

@ndueber and @Tomekmularczyk the issue you are describing is unrelated to Turbopack. Please open a separate issue, and fill the intake form and repro steps so we can better assist you.

Thanks, Ran~

@RanVaknin - This issue still happens if you are using webpack instead of turbopack with next’s app directory, so it does not make sense to wait until turbopack is “stable” before fixing.

Hi everyone - thank you for your comments.

It looks like the issue has been fixed and I was able to confirm it on my end as well. Please feel free to reach out again for anything related to AWS SDK for JavaScript. Closing issue now.

@joaopedrodcf Hey guys I also had this problem when using v5 of the lib, but after upgrading to aws-amplify v6 turbopack works without errors 💯

Same here, I’ve done the following upgrades and this problem has gone away:

Before

    "@aws-sdk/client-api-gateway": "^3.370.0",
    "@aws-sdk/client-lambda": "^3.548.0",
    "@aws-sdk/client-s3": "^3.332.0",
    "@aws-sdk/client-sfn": "^3.319.0",
    "@aws-sdk/client-sqs": "^3.328.0",
    "@aws-sdk/credential-provider-env": "^3.535.0",
    "@aws-sdk/s3-request-presigner": "^3.332.0",

After

    "@aws-sdk/client-api-gateway": "^3.583.0",
    "@aws-sdk/client-lambda": "^3.583.0",
    "@aws-sdk/client-s3": "^3.583.0",
    "@aws-sdk/client-sfn": "^3.583.0",
    "@aws-sdk/client-sqs": "^3.583.0",
    "@aws-sdk/s3-request-presigner": "^3.583.0"

Hey guys I also had this problem when using v5 of the lib, but after upgrading to aws-amplify v6 turbopack works without errors 💯

Hope this help others 💯

+1

Like @AllanZhengYP mentioned:

This is not a strict bug report since Turbopack hasn’t been stablized yet. However, it’s the minor issues in the AWS SDK blocking the Turbopack.

We will look into it when Turbopack becomes stable.

@ndueber and @Tomekmularczyk the issue you are describing is unrelated to Turbopack. Please open a separate issue, and fill the intake form and repro steps so we can better assist you.

Thanks, Ran~