next.js: [NEXT-1173] use node module inside instrumentation hook cause module not found error
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: #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
"browser": {
"path": false
}
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
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 4
- Comments: 18
Ran into this issue with 13.5.4, originally, I had
but apparently this does not work, instead it must be
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. Seen your codesandbox and this should fix it:
instrumentation.ts:
Hope this helps
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’m running into this in dependencies.
If I attempt to
await import('winston')
, it errors onos
being required by the@colors/colors
dependency inwinston
.Forked from @justLuiz’s example:
Just importing
winston
I have the same problem, This looks like a bug.
it works,thanks! maybe the document should describe it more clear
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
register
, but this is not ideal.Also running into this issue when attempting to bootstrap the https://github.com/microsoft/ApplicationInsights-node.js package.
Inlining the logic into the
register
function doesn’t work as the Node runtime dependencies are only used by theapplicationinsights
package. Using the workaround with thebrowser
config is a non-starter as that would require explicitly declaring[node package name]: false
for over a dozen packages, when those packages should be resolvable in thenodejs
runtime without issue.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:
If you export a function named register from this file, we will call that function whenever a new Next.js server instance is bootstrapped.