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

Build error "Cannot get final name for export '<export_name>'" when using server actions a certain way #60807

Closed
1 task done
mkreuzmayr opened this issue Jan 18, 2024 · 25 comments
Labels
bug Issue was opened via the bug report template. locked Module Resolution Module resolution (CJS / ESM, module resolving).

Comments

@mkreuzmayr
Copy link

Link to the code that reproduces this issue

https://github.com/mkreuzmayr/next-server-action-bug-repro/

To Reproduce

Run pnpm build to reproduce the following error

> next build

   ▲ Next.js 14.0.4

Failed to compile.

./src/server/auth/keycloak.ts + 8 modules
Cannot get final name for export 'sha256' of ./node_modules/.pnpm/oslo@0.27.1/node_modules/oslo/dist/crypto/index.js


> Build failed because of webpack errors
   Creating an optimized production build  . ELIFECYCLE  Command failed with exit code 1.

Explanation:

The file src/server/auth/keycloak.ts is imported in through src/server/auth/index.ts into the server action src/server/actions/test.ts.

I am unsure how and why this happens but there are two unrelated things I found you can do to make the error go away.

  1. Add a second page/route where the server action src/server/actions/test.ts is imported. Removing the trailing underscore from app/test2/_page.tsx makes the error go away.
  2. Commenting out the export on line 7 in src/server/auth/index.ts also makes the error go away.

Current vs. Expected behavior

Building the next app raises an error when it should actually build the app as normal.

Verify canary release

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

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
Binaries:
  Node: 20.10.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.0.5-canary.65
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Module resolution (CJS / ESM, module resolving)

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

The bug still exists on 14.0.5-canary.65

@mkreuzmayr mkreuzmayr added the bug Issue was opened via the bug report template. label Jan 18, 2024
@github-actions github-actions bot added the Module Resolution Module resolution (CJS / ESM, module resolving). label Jan 18, 2024
@mkreuzmayr
Copy link
Author

maybe related to #58756

@skve
Copy link

skve commented Feb 1, 2024

Seems to occur when importing certain (?) packages (for me, it was heroicons) into a component that was using useRef but not "use client". Adding "use client"; to those components resolved the build error

@mkreuzmayr
Copy link
Author

@skve You mean that this is an additional way to reproduce right? Because I am not using any imported client components from a package in my example. It would help if you could create a reproduction repo of this too.

@gavacq
Copy link

gavacq commented Feb 7, 2024

I'm experiencing a similar error:
./node_modules/.pnpm/@aws-crypto+sha256-js@5.2.0/node_modules/@aws-crypto/sha256-js/build/module/index.js + 12 modules Cannot get final name for export 'fromUtf8' of ./node_modules/.pnpm/@smithy+util-utf8@2.1.1/node_modules/@smithy/util-utf8/dist-es/index.js

I'm importing a server action in a client component. Everything marked as use server/client appropriately. When I remove this block from my server action, the build succeeds.

  try {
    await runWithAmplifyServerContext({
      nextServerContext: { cookies },
      operation: (contextSpec) => fetchUserAttributes(contextSpec),
    })
  } catch (error) {
    console.error(error)
    redirect('/login')
  }

This code is recommended by AWS Amplify in a SSR environment.

Related: aws-amplify/amplify-js#12856

@jelling
Copy link

jelling commented Feb 19, 2024

Same issue here with jimp

@bhaveshabuild
Copy link

Same issue as @gavacq. Its coming after adding AWS Amplify in SSR. Any resolution ?

@bhaveshabuild
Copy link

bhaveshabuild commented Feb 22, 2024

This is what worked for me:
https://www.sanity.media/p/65a2421194f7a8782500f775-how-i-fixed-aws-crypto-build-error
still a weird issue, looking for a better solution

Edit: The build error is gone but now I am getting this error while calling the fetchAuthSession method

Attempted to get the Amplify Server Context that may have been destroyed.

@bhaveshabuild
Copy link

bhaveshabuild commented Feb 22, 2024

Ultimately its not working as expected, did anyone find a solution for this ?

@bestickley
Copy link

For me, this issue occurred because of a circular dependency issue using barrel files. Once I changed my import from the index.ts (barrel file) to the actual file - the issue resolved.

@kaggwe-marvin
Copy link

I ran into the same issue, i was importing some utils. but read up on this in the docs...The arguments and return value of Server Actions must be serializable by React. See the React docs for a list of serializable arguments and values

@jelling
Copy link

jelling commented Mar 21, 2024

@gavacq's method of switching to require() fix this issue for me the second time I had it. Iirc the first time I just removed the library.

Perhaps there is a logging error that would help us provide more data? The issue seems related to project structure so duplicating that may be tough otherwise.

@MaximilianSchon
Copy link

For me, this happened when importing a turborepo package into a server action.

@jelling
Copy link

jelling commented Mar 23, 2024

For me, this happened when importing a turborepo package into a server action.

There might be something here. For me it's happening when importing NX packages. I'm also having a bizarre issue where at runtime the server action suddenly can't find the function: #63591

Overall, it feels like we just need more transparency into what is happening in the server action build process. As edge case build issues seem to be at the root of all of these server action issues.

@harryhaibojiang
Copy link

harryhaibojiang commented Mar 23, 2024

I've found a solution here worked for me: #59344 (comment)
adding the package into next.config.js :
experimental: { serverComponentsExternalPackages: [ '@aws-amplify/adapter-nextjs', 'aws-amplify' ] }

@paluchi
Copy link

paluchi commented Apr 19, 2024

I've found a solution here worked for me: #59344 (comment) adding the package into next.config.js : experimental: { serverComponentsExternalPackages: [ '@aws-amplify/adapter-nextjs', 'aws-amplify' ] }

The goat

@pedroSoaresll
Copy link

pedroSoaresll commented Apr 21, 2024

I've found a solution here worked for me: #59344 (comment) adding the package into next.config.js : experimental: { serverComponentsExternalPackages: [ '@aws-amplify/adapter-nextjs', 'aws-amplify' ] }

helped much!

In my case it was only needed to put the aws-amplify in the serverComponentsExternalPackages array.

@chriszlr
Copy link

Does anyone have this error with the openai package?
./utils/helpers.ts + 196 modules
Cannot get final name for export 'FormDataEncoder' of ./node_modules/openai/node_modules/form-data-encoder/lib/esm/index.js

experimental: { serverComponentsExternalPackages: [ '@aws-amplify/adapter-nextjs', 'aws-amplify' ] }
doesnt fix it for me

@adambarito
Copy link

adambarito commented Apr 30, 2024

Does anyone have this error with the openai package? ./utils/helpers.ts + 196 modules Cannot get final name for export 'FormDataEncoder' of ./node_modules/openai/node_modules/form-data-encoder/lib/esm/index.js

experimental: { serverComponentsExternalPackages: [ '@aws-amplify/adapter-nextjs', 'aws-amplify' ] } doesnt fix it for me

I had the same issue with openai (via langchain). Adding experimental: {serverComponentsExternalPackages: ['langchain','@langchain/openai',],}, fixed my issue.

I'm guessing experimental: { serverComponentsExternalPackages: [ 'openai' ] } will do what you want.

Thank you @harryhaibojiang!

@tanapoln
Copy link

I got a similar error and I'm able to reproduce this. Please check my repo https://github.com/tanapoln/nextjs-build-error

Still don't know how to fix it.

@Alex01d
Copy link

Alex01d commented Jun 12, 2024

It's working for me with Next 15.0.0-canary.25+, so waiting for a stable version.

@mkreuzmayr
Copy link
Author

@Alex01d Thanks for the info! I just checked and it is also working with 14.3.0-canary.46.

Thank you for all the workarounds, I am closing this for now.

@HuiSF
Copy link

HuiSF commented Jun 18, 2024

Hi @Alex01d when you say it's working, you meant it worked without specifying the serverComponentsExternalPackages in next.config.js? Thanks in advance!

@mkreuzmayr
Copy link
Author

Hi @Alex01d when you say it's working, you meant it worked without specifying the serverComponentsExternalPackages in next.config.js? Thanks in advance!

For me it is working without specifying it in serverComponentsExternalPackages.

@Alex01d
Copy link

Alex01d commented Jun 18, 2024

Hi @Alex01d when you say it's working, you meant it worked without specifying the serverComponentsExternalPackages in next.config.js? Thanks in advance!

I had this issue with 2 imports: 'jose' and 'next/navigation'. serverComponentsExternalPackages is working with 'jose' even in v14.2, but 'next/navigation' isn't. Both are working in v15 without serverComponentsExternalPackages.

Copy link
Contributor

github-actions bot commented Jul 3, 2024

This closed issue has been automatically locked because it had no new activity for 2 weeks. 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 Jul 3, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Module Resolution Module resolution (CJS / ESM, module resolving).
Projects
None yet
Development

No branches or pull requests