Closed ck-euan closed 2 months ago
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 build
with:
./node_modules/.pnpm/@mswjs+interceptors@0.35.6/node_modules/@mswjs/interceptors/lib/node/chunk-RWGRRMVU.mjs
Module not found: Can't resolve '_http_common'
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 next@14.2.11 and msw@2.4.8.
Thanks for reporting this, @ck-euan.
_http_common
is a built-in module in Node.js. I wonder why Next.js has problem importing it.
node
require('_http_common')
// ...see exports.
This is likely a bug on Next.js side (the _http_common
is sort of internal, but it exists, and mustn't throw when importing it).
There's an exact issue about it from long ago (https://github.com/vercel/next.js/issues/30091) but it doesn't have any resolution.
Opened an issue with Next.js: https://github.com/vercel/next.js/issues/70262. Until proven otherwise, this has nothing to do with MSW. Please track the progress in that issue. Thanks.
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 next@14.2.11 and msw@2.4.8.
- node v22.7.0
- pnpm v9.9.0
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 :)
As the error message suggests, the issue arises because the module "_http_common"
could not be found. This error is related to MSW (Mock Service Worker) using the @mswjs/interceptors
library in Node.js environments to intercept network requests, where the internal _http_common
module is used. However, Next.js's default webpack configuration does not handle this module, resulting in this error.
Module not found: Can't resolve '_http_common'
: This error occurs because _http_common
is a built-in Node.js module, but Next.js's webpack configuration does not automatically include this module in the bundle./** @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;
if (isServer)
:
config.externals = [...(config.externals || []), '_http_common']
:
"_http_common"
module to externals
, meaning webpack will treat it as an external module and not include it in the bundle.externals
are not bundled but are instead handled by Node.js at runtime.config.target = 'node'
:
target
to 'node'
ensures that the bundle is optimized for execution in Node.js environments._http_common
could not be found is that webpack does not automatically handle this module._http_common
is added to externals
, and the webpack target
is set to node
, allowing Node.js to handle this module at runtime.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:
canary
if you don't want to wait.
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