next.js: Build error "Cannot get final name for export ''" when using server actions a certain way

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

About this issue

  • Original URL
  • State: open
  • Created 5 months ago
  • Reactions: 6
  • Comments: 16

Most upvoted comments

I’ve found a solution here worked for me: https://github.com/vercel/next.js/issues/59344#issuecomment-1846128759 adding the package into next.config.js : experimental: { serverComponentsExternalPackages: [ '@aws-amplify/adapter-nextjs', 'aws-amplify' ] }

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.

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: https://github.com/aws-amplify/amplify-js/issues/12856

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