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

A module can be imported multiple times on server #55885

Open
1 task done
duydang2311 opened this issue Sep 23, 2023 · 7 comments
Open
1 task done

A module can be imported multiple times on server #55885

duydang2311 opened this issue Sep 23, 2023 · 7 comments
Labels
bug Issue was opened via the bug report template.

Comments

@duydang2311
Copy link

duydang2311 commented Sep 23, 2023

Link to the code that reproduces this issue

https://github.com/duydang2311/module-imported-multiple-times-issue-reproduction

To Reproduce

  1. Start the application in development (next dev).
  2. Connect to http://localhost:3000.

Current vs. Expected behavior

There are instrumentation.ts (NodeJS runtime) and page.tsx (runs on server) importing the same service.ts file.

Current behavior

The server prints "service.ts was imported" twice.

This behaviour, in my original code, causes my service configuration in instrumentation hook to be lost when service.ts is imported and used in my pages.

Expected behavior

The server prints "service.ts was imported" just once.

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Fri Jan 27 02:56:13 UTC 2023
Binaries:
  Node: 18.18.0
  npm: 10.1.0
  Yarn: N/A
  pnpm: 8.7.5
Relevant Packages:
  next: 13.5.3-canary.3
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

App Router

Additional context

No response

@duydang2311 duydang2311 added the bug Issue was opened via the bug report template. label Sep 23, 2023
@justLuiz
Copy link

for me this is not a bug, in your sample its a expected behavior

@duydang2311
Copy link
Author

I think the expected behaviour is to load/initialize every module just once in the same environment, which is on the server in this sample. I'm using instrumentation as an entry point to configure my services. Once the pages import the service, it somehow treats the service as a new module as if it was never imported before, hence all the pre-configuration are lost at this point.

@MangoMarcus
Copy link

Is there any update on this issue?

Modules should only be evaluated once, so this isn't the expected behavior. OP's example includes a dynamic import which may throw a spanner in the works, but it should still only be evaluated once.

I've noticed the same behavior with static imports both during dev and during the build step, using the app router.

@leduyquang753
Copy link

leduyquang753 commented Apr 14, 2024

Having the same issue. Setting up a websocket instance in a module causes multiple instances to be created on the server. So I have to resort to setting things into globalThis.

@VictorGlindasPaf
Copy link

This seems to happen both in dev and production mode.
The cause looks to be due to multiple chunks containing the same module code.

@MangoMarcus

This comment has been minimized.

@PrinOrange
Copy link

Same here. And now I'm trapped in such testing problem for long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

6 participants