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

"Module not found" warnings when importing AWS s3 in app directory #51969

Closed
1 task done
rijk opened this issue Jun 29, 2023 · 4 comments
Closed
1 task done

"Module not found" warnings when importing AWS s3 in app directory #51969

rijk opened this issue Jun 29, 2023 · 4 comments
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. locked

Comments

@rijk
Copy link

rijk commented Jun 29, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.5.0: Thu Jun  8 22:21:34 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T8112
    Binaries:
      Node: 16.18.1
      npm: 8.19.2
      Yarn: N/A
      pnpm: 8.6.3
    Relevant Packages:
      next: 13.4.8-canary.8
      eslint-config-next: 13.4.7
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true), Package manager (npm, pnpm, Yarn)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/rijk/repro-s3-import-issue

To Reproduce

  1. Add an S3 user / key to the .env file (I'm sorry, I tried adding credentials here but AWS didn't let me)
  2. Run the app
  3. Go to http://localhost:3000/api/test-page
  4. Observe signed URL returned without warnings
  5. Go to http://localhost:3000/api/test-app
  6. Signed URL returned, but with a bunch of Module not found warnings in the console

Describe the Bug

When importing from the s3 SDK in the app directory (either in a route handler or server action), this triggers a bunch of warnings (see below). This doesn't happen in the pages directory. Is module resolution different between the two?

Warnings:
- warn ./node_modules/.pnpm/@aws-sdk+signature-v4-multi-region@3.357.0/node_modules/@aws-sdk/signature-v4-multi-region/dist-cjs/SignatureV4MultiRegion.js
Module not found: Can't resolve '@aws-sdk/signature-v4-crt' in '/Users/Rijk/Code/Playground/next/repro-s3-import-issue/node_modules/.pnpm/@aws-sdk+signature-v4-multi-region@3.357.0/node_modules/@aws-sdk/signature-v4-multi-region/dist-cjs'

Import trace for requested module:
./node_modules/.pnpm/@aws-sdk+signature-v4-multi-region@3.357.0/node_modules/@aws-sdk/signature-v4-multi-region/dist-cjs/SignatureV4MultiRegion.js
./node_modules/.pnpm/@aws-sdk+signature-v4-multi-region@3.357.0/node_modules/@aws-sdk/signature-v4-multi-region/dist-cjs/index.js
./node_modules/.pnpm/@aws-sdk+client-s3@3.362.0/node_modules/@aws-sdk/client-s3/dist-cjs/runtimeConfig.shared.js
./node_modules/.pnpm/@aws-sdk+client-s3@3.362.0/node_modules/@aws-sdk/client-s3/dist-cjs/runtimeConfig.js
./node_modules/.pnpm/@aws-sdk+client-s3@3.362.0/node_modules/@aws-sdk/client-s3/dist-cjs/S3Client.js
./node_modules/.pnpm/@aws-sdk+client-s3@3.362.0/node_modules/@aws-sdk/client-s3/dist-cjs/index.js
./app/api/test-app/route.ts

./node_modules/.pnpm/@aws-sdk+util-user-agent-node@3.357.0/node_modules/@aws-sdk/util-user-agent-node/dist-cjs/is-crt-available.js
Module not found: Can't resolve 'aws-crt' in '/Users/Rijk/Code/Playground/next/repro-s3-import-issue/node_modules/.pnpm/@aws-sdk+util-user-agent-node@3.357.0/node_modules/@aws-sdk/util-user-agent-node/dist-cjs'

Import trace for requested module:
./node_modules/.pnpm/@aws-sdk+util-user-agent-node@3.357.0/node_modules/@aws-sdk/util-user-agent-node/dist-cjs/is-crt-available.js
./node_modules/.pnpm/@aws-sdk+util-user-agent-node@3.357.0/node_modules/@aws-sdk/util-user-agent-node/dist-cjs/index.js
./node_modules/.pnpm/@aws-sdk+client-s3@3.362.0/node_modules/@aws-sdk/client-s3/dist-cjs/runtimeConfig.js
./node_modules/.pnpm/@aws-sdk+client-s3@3.362.0/node_modules/@aws-sdk/client-s3/dist-cjs/S3Client.js
./node_modules/.pnpm/@aws-sdk+client-s3@3.362.0/node_modules/@aws-sdk/client-s3/dist-cjs/index.js
./app/api/test-app/route.ts

Expected Behavior

The code should run without warnings, as it does in the pages directory.

Which browser are you using? (if relevant)

not relevant

How are you deploying your application? (if relevant)

not relevant

@rijk rijk added the bug Issue was opened via the bug report template. label Jun 29, 2023
@github-actions github-actions bot added the area: app App directory (appDir: true) label Jun 29, 2023
@rijk rijk changed the title AWS s3 import module not found warnings in app directory "Module not found" warnings when importing AWS s3 in app directory Jun 29, 2023
@balazsorban44
Copy link
Member

balazsorban44 commented Jul 6, 2023

Hi, this is similar to #52091 (comment)

The current solution is:

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: ['@aws-sdk/client-s3'],
  },
}

module.exports = nextConfig

@rijk
Copy link
Author

rijk commented Jul 7, 2023

That was it, thank you 🙏

For me specifically, I had to add two packages:

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: [
      '@aws-sdk/client-s3',
      '@aws-sdk/s3-presigned-post',
    ],
  },
}

module.exports = nextConfig

Just a note, and not meant as criticism but it might save you work in the future: I've read that documentation page in the past, but I honestly never connected it to this type of issue. Maybe it would be good to expand this page with:

  1. An explanation why "Dependencies will automatically be bundled by Next.js", how this works technically and what the benefit is;
  2. What issues you might encounter which indicate that you'd need to exclude them (maybe some example error messages, so people Googling those will find this page).

Especially because for popular packages like Prisma this is done automatically, it can be extra confusing when it's not working.

@balazsorban44
Copy link
Member

Thanks! We are discussing this internally to improve both the messaging, and hopefully also the automatic detection.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 7, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Aug 7, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. locked
Projects
None yet
Development

No branches or pull requests

2 participants