-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
[NEXT-1173] use node module inside instrumentation hook cause module not found error #49565
Comments
Hi, was trying this feature out and faced the same problem at first. Then read again the documentation and realized that I needed to use import inside the function. The other issue is that you need to check that your are running your code in the correct runtime. instrumentation.ts:
Hope this helps |
My problem with this feature is I wanted something to run on server start and not when the first browser window opens. It seems that its not the case even though they are marketing it as start with the server: |
it works,thanks! |
I'm also hitting a similar issue:
For my case, I'm running my DB migrations on server start, and I import the migration function from my DB lib. From the above solution, it looks like I may need to pull in my DB connection and migration logic directly into |
I have the same problem, This looks like a bug. |
Also running into this issue when attempting to bootstrap the https://github.com/microsoft/ApplicationInsights-node.js package. Inlining the logic into the |
@lmgeorge Have you managed to fix or implement some workaround for this issue? I'm also trying to include the |
@kubop I have not. I started to explore the workaround mentioned in the description of the bug, but after adding the tenth Node standard library package, I gave up due to time constraints. |
FWIW, this doesn't seem to work in my 13.4.19, at least with webpack config:
|
Ran into this issue with 13.5.4, originally, I had if (process.env.NEXT_RUNTIME !== "nodejs") return;
// imports and do something but apparently this does not work, instead it must be if (process.env.NEXT_RUNTIME === "nodejs") {
// imports and do something
} |
It worked for me on 13.4.19. Thank you! |
Another usefull sample with this problem |
I'm running into this in dependencies. If I attempt to Forked from @justLuiz's example: |
for some reason...found out that you must put those imports in:
not only using await, the trick seems to be "if (process.env.NEXT_RUNTIME === "nodejs") {...}", you have to do things inside this "IF", I don't know why, but it worked well, otherwise didn't. |
I had the same issue and this tip did the trick to solve the issue. Thanks |
this is true, I tried both with/without full env path like below (using next 14.1) // does not work
const runtime = process.env.NEXT_RUNTIME
if (runtime === "nodejs") {
const { myModule} = await import("some-module")
} and only below works // works
if (process.env.NEXT_RUNTIME=== "nodejs") {
const { myModule } = await import("some-module")
} seems like nextjs under the hook make a detection pattern only working with fully annotated process.env. felt like kind of unneeded complicates tho |
Can also confirm that an early return won't work
|
for me
But I am unable to see any consoles. Do consoles don't work here? Or should i do some extra steps to make |
here is what i did - i wanted to execute functions in my next.js application on every startup - i tried using export function register() {
fetch('http://localhost:3000/api/init')
} i have hard coded the URL, but you can use |
This is too cumbersome to use for initialization. I ended up moving all the init code into another node service altogether, based on Express, where it's much easier. |
I encountered this error with several libraries.
Any other workflow will potentially give error, so avoid writing literally anything else in instrumentation file. |
I ended up using magic comments to disable webpack for my import:
My problem was, that my imported path was always a dynamic variable and did not find a valid solution to import it. However, I guess there isa way but I did not spend much more time with it and just added webpackIgnore comment. I do not think that there will be crazy negative aspects when you use it for dynamic imports. But of course it is not the cleanest solution. Maybe somebody with more webpack expierence has a more clean solution. instrumentation.ts
bootstrap.ts
|
This is caused by the runtime since when the One small improvement we patched on latest canary release is that we won't generate the edge runtime entry of instrumentation.js when you only have Node.js routes, so you can safely write Node.js code inside of the |
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. |
Verify canary release
Provide environment information
Operating System: Platform: linux Arch: x64 Version: #22 SMP Tue Jan 10 18:39:00 UTC 2023 Binaries: Node: 16.17.0 npm: 8.15.0 Yarn: 1.22.19 pnpm: 7.1.0 Relevant packages: next: 13.4.2-canary.4 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 4.9.4
Which area(s) of Next.js are affected? (leave empty if unsure)
No response
Link to the code that reproduces this issue
https://codesandbox.io/p/sandbox/awesome-roman-jkuyps?file=%2Fpages%2Findex.tsx%3A3%2C10-3%2C14
To Reproduce
run yarn dev in codesandbox
Describe the Bug
use node module like path (except process, because it has been defined in webpack.resolve.fallback) inside instrumentation.ts file will cause module not found error
also, it can be temporarily solved by adding browser field in package.json
Expected Behavior
no error since it's a server startup config, it's highly likely to use nodejs module(s)
Which browser are you using? (if relevant)
Chrome 100.0.4878.0
How are you deploying your application? (if relevant)
next start
NEXT-1173
The text was updated successfully, but these errors were encountered: