-
Notifications
You must be signed in to change notification settings - Fork 86
fix: add require shims #2050
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
fix: add require shims #2050
Conversation
✅ Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-export-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-plugin-canary ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for nextjs-plugin-custom-routes-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-i18next-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-plugin-edge-middleware ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
I think adding |
Just adding https://github.com/vercel/next.js/pull/47191/files#diff-be23f4ad09849e271e6c9f4f373142bc18167eb9f4fe6a86f8705b80e07995d3R202-R211 for some reference - I'm not sure of where Also above explains how we end up with that require (externals -> commonjs in webpack config) and also gives potential hint that if we don't want to produce code with |
I think maybe this means Rob was actually right, and we should take the same approach. Put that ghastly |
does anyone have any insight into why Vercel pick specific properties out of the native modules? https://github.com/vercel/next.js/pull/47191/files#diff-be23f4ad09849e271e6c9f4f373142bc18167eb9f4fe6a86f8705b80e07995d3R149-R156 |
No idea why they omitted some. |
Thanks @ascorbic, that's really useful. We can import the |
require('node:buffer')
Are you planning to add shims for the other stuff too? |
if (name === 'buffer' || name === 'node:buffer') { | ||
return { Buffer: BufferCompat } | ||
} | ||
throw new ReferenceError('require is not defined') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll seem weird if we get the error require is not defined
given that require
is defined. Consdier an error message that mentions a particular module cannot be pulled in via require as that will signal to us that another shim is required.
This will help with E2E canary test runs to signal earlier on exactly what the issue is.
Something along these lines.
throw new ReferenceError('require is not defined') | |
throw new ReferenceError(`Unable to require module ${name}. A shim for module ${name} may be missing.`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about matching the Next.js message? throw TypeError('Native module not found: ' + id)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even better 😎
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you planning to add shims for the other stuff too?
Do you mean the other stuff in the NativeModuleMap
, @ascorbic? I wasn't certain whether that was specific to Vercel's infrastructure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're all the Node modules that are available in workerd, so which Vercel edge runtime now assumes are available. Buffer is the only on they're using in core Next.js right now (I think), but they're basically telling users that they can use them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ascorbic okay, I'll shim all the things!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great @orinokai!
@@ -48,6 +52,26 @@ const fetch /* type {typeof globalThis.fetch} */ = async (url, init) => { | |||
} | |||
} | |||
|
|||
// Shim native modules that Vercel makes available | |||
if (typeof require === 'undefined') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So many shims. 😅
require('node:buffer')
Summary
A new turbopack feature allows free variables to be aliased to modules, which causes a Next.js reference to
Buffer
to be bundled asrequire("node:buffer")
, which is not supported by Deno.This PR adds a shim for require that specifically loads
Buffer
for this case only.Test plan
Incoming...
Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal
Fixes #2047