-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
Bug: Module not found: Can't resolve '_http_common' in Next.js #2291
Comments
Can confirm, same issue (I tested on node 18, pnpm 9, any NextJS 14.2 patch release including latest). MSW <= 2.4.3 works but any MSW version >= 2.4.4 fails during nextjs
|
next.config.mjs /** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config, { isServer }) => {
if (isServer) {
config.externals = [...(config.externals || []), '_http_common'];
config.target = 'node';
}
return config;
}
};
export default nextConfig; This worked with [email protected] and [email protected].
|
Thanks for reporting this, @ck-euan.
node
require('_http_common')
// ...see exports. This is likely a bug on Next.js side (the There's an exact issue about it from long ago (vercel/next.js#30091) but it doesn't have any resolution. |
Opened an issue with Next.js: vercel/next.js#70262. Until proven otherwise, this has nothing to do with MSW. Please track the progress in that issue. Thanks. |
This workaround works at the moment. (node.js v22.6.0, "next": "14.2.13", "msw": "^2.4.9") |
The following is written by my GPT4o :) Error AnalysisAs the error message suggests, the issue arises because the module
Explanation of the webpack Configuration/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
instrumentationHook: true
},
webpack: (config, { isServer }) => {
// Apply configuration only in the server environment
if (isServer) {
// Add '_http_common' to config.externals to treat it as an external module
config.externals = [...(config.externals || []), '_http_common'];
// Set webpack target to 'node' to support building for Node.js environments
config.target = 'node';
}
return config;
}
}
export default nextConfig; Explanation of the webpack Code:
Summary of the Solution
This configuration resolves the issue with Node.js built-in module errors related to MSW, allowing you to use MSW handlers without errors on the Next.js server. |
I will make this abundantly clear for everyone: This has been addressed in Next.js. Please wait or the next version and update to it, the fix is there. You can also use
|
Prerequisites
Environment check
msw
versionNode.js version
v20.17.0
Reproduction repository
https://github.com/ck-euan/msw-bug-example
Reproduction steps
dev
script to start the Next.js app and you should see the error in the console.dev
script and the issue isn't thereCurrent behavior
After upgrading to 2.4.4 and above, I encounter the following error when running my Next.js app:
Expected behavior
The app should run without errors with the msw server running via the Next.js instrumentation hook
The text was updated successfully, but these errors were encountered: