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

Server actions: Cannot use classes imported from ESM packages included in serverComponentsExternalPackages option #60756

Closed
1 task done
pilcrowonpaper opened this issue Jan 17, 2024 · 3 comments · Fixed by #65041
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Module Resolution Module resolution (CJS / ESM, module resolving).

Comments

@pilcrowonpaper
Copy link

pilcrowonpaper commented Jan 17, 2024

Link to the code that reproduces this issue

https://github.com/pilcrowOnPaper/next-server-actions-esm-class-repro

To Reproduce

  1. Start the dev server
  2. (Click the button on page /)
  3. Observe the error in the console

Current vs. Expected behavior

I can't seem to use classes imported from ESM packages included in experimental serverComponentsExternalPackages option in server actions, when the action is imported from client components.

This works fine when the package is CJS, when the package is not included in serverComponentsExternalPackages, or when the action is passed down to the client component as a prop.

Verify canary release

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

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 23.2.0: Wed Nov 15 21:54:10 PST 2023; root:xnu-10002.61.3~2/RELEASE_X86_64
Binaries:
  Node: 20.2.0
  npm: 9.6.6
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 14.0.5-canary.60
  eslint-config-next: 14.0.4
  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)

App Router, Module resolution (CJS / ESM, module resolving)

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

next dev (local), next build (local)

Additional context

I need to use serverComponentsExternalPackages as certain modules (oslo/password) require Node specific modules (@node-rs/argon2)

NEXT-2435

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

Here's the full error by the way:

 ⨯ actions/index.ts (5:14) @ action
 ⨯ TypeError: oslo__WEBPACK_IMPORTED_MODULE_2__.TimeSpan is not a constructor
    at action (./actions/index.ts:14:17)
  3 |
  4 | export async function action() {
> 5 |   console.log(new TimeSpan(30, "d").seconds());
    |              ^
  6 | }
  7 |
 ⨯ actions/index.ts (5:14) @ action
 ⨯ TypeError: oslo__WEBPACK_IMPORTED_MODULE_2__.TimeSpan is not a constructor
    at action (./actions/index.ts:14:17)
  3 |
  4 | export async function action() {
> 5 |   console.log(new TimeSpan(30, "d").seconds());
    |              ^
  6 | }
  7 |

@pilcrowonpaper
Copy link
Author

pilcrowonpaper commented Jan 17, 2024

Looks like inlining the import fixes the issue

new (await import("oslo/password")).Argon2id();

@feedthejim feedthejim added the linear: next Confirmed issue that is tracked by the Next.js team. label Feb 12, 2024
huozhi added a commit that referenced this issue May 7, 2024
### What

Support `esmExternals` working in app router

### Why

`esmExternals` was disabled for app router that most of the packages are
picking up the CJS bundles for externals. This PR enables to resolve the
ESM bundle for external packages.

We have two issues discovered while enabling the flag, any esm external
packages will fail in client page SSR and server action. We fixed them
by changing the below bundling logics:

* When a client page having a async dependency, we can await the page
during in rendering
* When a server action having a async dependency, we changed the server
action entry creation with webpack along with the server client entry
creation together, then webpack can handle the modules async propagation
properly.


Fixes #60756 
Closes NEXT-2435
Closes NEXT-2472
Closes NEXT-3225

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
Copy link
Contributor

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 locked as resolved and limited conversation to collaborators May 22, 2024
lubieowoce pushed a commit that referenced this issue Aug 23, 2024
Support `esmExternals` working in app router

`esmExternals` was disabled for app router that most of the packages are
picking up the CJS bundles for externals. This PR enables to resolve the
ESM bundle for external packages.

We have two issues discovered while enabling the flag, any esm external
packages will fail in client page SSR and server action. We fixed them
by changing the below bundling logics:

* When a client page having a async dependency, we can await the page
during in rendering
* When a server action having a async dependency, we changed the server
action entry creation with webpack along with the server client entry
creation together, then webpack can handle the modules async propagation
properly.

Fixes #60756
Closes NEXT-2435
Closes NEXT-2472
Closes NEXT-3225

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
lubieowoce pushed a commit that referenced this issue Aug 23, 2024
Support `esmExternals` working in app router

`esmExternals` was disabled for app router that most of the packages are
picking up the CJS bundles for externals. This PR enables to resolve the
ESM bundle for external packages.

We have two issues discovered while enabling the flag, any esm external
packages will fail in client page SSR and server action. We fixed them
by changing the below bundling logics:

* When a client page having a async dependency, we can await the page
during in rendering
* When a server action having a async dependency, we changed the server
action entry creation with webpack along with the server client entry
creation together, then webpack can handle the modules async propagation
properly.

Fixes #60756
Closes NEXT-2435
Closes NEXT-2472
Closes NEXT-3225

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
lubieowoce pushed a commit that referenced this issue Aug 23, 2024
Support `esmExternals` working in app router

`esmExternals` was disabled for app router that most of the packages are
picking up the CJS bundles for externals. This PR enables to resolve the
ESM bundle for external packages.

We have two issues discovered while enabling the flag, any esm external
packages will fail in client page SSR and server action. We fixed them
by changing the below bundling logics:

* When a client page having a async dependency, we can await the page
during in rendering
* When a server action having a async dependency, we changed the server
action entry creation with webpack along with the server client entry
creation together, then webpack can handle the modules async propagation
properly.

Fixes #60756
Closes NEXT-2435
Closes NEXT-2472
Closes NEXT-3225

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
lubieowoce pushed a commit that referenced this issue Aug 23, 2024
Support `esmExternals` working in app router

`esmExternals` was disabled for app router that most of the packages are
picking up the CJS bundles for externals. This PR enables to resolve the
ESM bundle for external packages.

We have two issues discovered while enabling the flag, any esm external
packages will fail in client page SSR and server action. We fixed them
by changing the below bundling logics:

* When a client page having a async dependency, we can await the page
during in rendering
* When a server action having a async dependency, we changed the server
action entry creation with webpack along with the server client entry
creation together, then webpack can handle the modules async propagation
properly.

Fixes #60756
Closes NEXT-2435
Closes NEXT-2472
Closes NEXT-3225

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
lubieowoce pushed a commit that referenced this issue Aug 28, 2024
Support `esmExternals` working in app router

`esmExternals` was disabled for app router that most of the packages are
picking up the CJS bundles for externals. This PR enables to resolve the
ESM bundle for external packages.

We have two issues discovered while enabling the flag, any esm external
packages will fail in client page SSR and server action. We fixed them
by changing the below bundling logics:

* When a client page having a async dependency, we can await the page
during in rendering
* When a server action having a async dependency, we changed the server
action entry creation with webpack along with the server client entry
creation together, then webpack can handle the modules async propagation
properly.

Fixes #60756
Closes NEXT-2435
Closes NEXT-2472
Closes NEXT-3225

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
lubieowoce pushed a commit that referenced this issue Sep 2, 2024
Support `esmExternals` working in app router

`esmExternals` was disabled for app router that most of the packages are
picking up the CJS bundles for externals. This PR enables to resolve the
ESM bundle for external packages.

We have two issues discovered while enabling the flag, any esm external
packages will fail in client page SSR and server action. We fixed them
by changing the below bundling logics:

* When a client page having a async dependency, we can await the page
during in rendering
* When a server action having a async dependency, we changed the server
action entry creation with webpack along with the server client entry
creation together, then webpack can handle the modules async propagation
properly.

Fixes #60756
Closes NEXT-2435
Closes NEXT-2472
Closes NEXT-3225

---------

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
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. linear: next Confirmed issue that is tracked by the Next.js team. locked Module Resolution Module resolution (CJS / ESM, module resolving).
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants