Closed chfoidl closed 1 year ago
Sounds like it's a restriction of Edge Runtime. It should probably do this by default, but for an environment in which you know that Function.prototype.bind exists, you can replace function-bind
with module.exports = Function.bind
with your build tool, and that should resolve the problem.
See https://github.com/vercel/next.js/pull/52009 for that fix.
Thanks for pointing me in the right direction!
Here is what I did to get this working in NextJS:
Create a new file function-bind.cjs
:
module.exports = Function.bind;
Update webpack config in next.config.js
:
module.exports = {
// ...
webpack: (config, context) => {
if (context.nextRuntime === "edge") {=
// Assuming the function-bind.cjs file is in your
// NextJS project root.
config.resolve.alias["function-bind$"] =
require.resolve("./function-bind.cjs");
}
return config;
},
};
When using ESM for next.config.js
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
export default {
// ...
webpack: (config, context) => {
if (context.nextRuntime === "edge") {=
// Assuming the function-bind.cjs file is in your
// NextJS project root.
config.resolve.alias["function-bind$"] =
require.resolve("./function-bind.cjs");
}
return config;
},
};
Using the
qs
library or any other module that depends onqs
for that matter does not work when importing in a NextJS edge middleware.It results in the following error message:
I have created a tiny demo project that depicts this problem: https://codesandbox.io/p/sandbox/affectionate-turing-hy2rwl