-
Notifications
You must be signed in to change notification settings - Fork 27.3k
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
Comments
for me this is not a bug, in your sample its a expected behavior |
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. |
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. |
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 |
This seems to happen both in dev and production mode. |
This comment has been minimized.
This comment has been minimized.
Same here. And now I'm trapped in such testing problem for long time. |
Link to the code that reproduces this issue
https://github.com/duydang2311/module-imported-multiple-times-issue-reproduction
To Reproduce
Current vs. Expected behavior
There are
instrumentation.ts
(NodeJS runtime) andpage.tsx
(runs on server) importing the sameservice.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
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
The text was updated successfully, but these errors were encountered: