Closed brc-dd closed 2 years ago
I ended up doing:
const nodeVer = typeof process !== 'undefined' && process.versions?.node
const nodeRequire = nodeVer
? typeof __webpack_require__ === 'function'
? __non_webpack_require__
: require
: undefined
This closed issue has been automatically locked because it had no new activity for a month. 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
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Put something like this in
pages/_middleware.js
:It throws error like:
I know that we have access to
fetch
inside middlewares, but the question I want to ask is how can one write a module that works on both Node.js and edge-runtimes without making the users to manually specify the file to import?Expected Behavior
I expect Next.js to by default import
browser
field of package.json inside middlewares. Or don't check for native modules inside conditions that are not true on middlewares. For example in this one:EDIT:
I understand the reasoning behind not importing browser field as mentioned at https://github.com/vercel/next.js/issues/36844#issuecomment-1124358786. But for the second one (selective requiring/import), I think that's a bug.