Skip to content
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

Closed
4 tasks done
ck-euan opened this issue Sep 18, 2024 · 7 comments
Closed
4 tasks done

Bug: Module not found: Can't resolve '_http_common' in Next.js #2291

ck-euan opened this issue Sep 18, 2024 · 7 comments
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@ck-euan
Copy link

ck-euan commented Sep 18, 2024

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

v20.17.0

Reproduction repository

https://github.com/ck-euan/msw-bug-example

Reproduction steps

  1. Run the dev script to start the Next.js app and you should see the error in the console.
  2. Downgrade to msw 2.4.3 or below and run the dev script and the issue isn't there

Current behavior

After upgrading to 2.4.4 and above, I encounter the following error when running my Next.js app:

 ⨯ ./node_modules/.pnpm/@[email protected]/node_modules/@mswjs/interceptors/lib/node/chunk-RWGRRMVU.mjs:21:1
Module not found: Can't resolve '_http_common'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/.pnpm/@[email protected]/node_modules/@mswjs/interceptors/lib/node/interceptors/ClientRequest/index.mjs
./node_modules/.pnpm/[email protected][email protected]/node_modules/msw/lib/node/index.mjs
./tests/mocks/node.ts
./src/instrumentation.ts
Error: An error occurred while loading instrumentation hook: Cannot find module '/Users/euanmorgan/dev/msw-bug/.next/server/instrumentation'
Require stack:
- /Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/dev/next-dev-server.js
- /Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/next.js
- /Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/lib/start-server.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1225:15)
    at /Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/require-hook.js:55:36
    at Module._load (node:internal/modules/cjs/loader:1051:27)
    at Module.require (node:internal/modules/cjs/loader:1311:19)
    at mod.require (/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/require-hook.js:65:28)
    at require (node:internal/modules/helpers:179:18)
    at DevServer.runInstrumentationHookIfAvailable (/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/dev/next-dev-server.js:436:51)
    at async Span.traceAsyncFn (/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/trace/trace.js:154:20)
    at async DevServer.prepareImpl (/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/dev/next-dev-server.js:214:9)
    at async NextServer.prepare (/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/next.js:161:13)
    at async initializeImpl (/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/lib/render-server.js:98:5)
    at async initialize (/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/lib/router-server.js:420:22)
    at async Server.<anonymous> (/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/lib/start-server.js:249:36) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/dev/next-dev-server.js',
    '/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/next.js',
    '/Users/euanmorgan/dev/msw-bug/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/server/lib/start-server.js'
  ]
}

Expected behavior

The app should run without errors with the msw server running via the Next.js instrumentation hook

@ck-euan ck-euan added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Sep 18, 2024
@AlanSl
Copy link

AlanSl commented Sep 18, 2024

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/@[email protected]/node_modules/@mswjs/interceptors/lib/node/chunk-RWGRRMVU.mjs
Module not found: Can't resolve '_http_common'

@Seungwoo321
Copy link

Seungwoo321 commented Sep 19, 2024

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].

  • node v22.7.0
  • pnpm v9.9.0

@kettanaito
Copy link
Member

kettanaito commented Sep 19, 2024

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 (vercel/next.js#30091) but it doesn't have any resolution.

@kettanaito
Copy link
Member

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.

@Sly777
Copy link

Sly777 commented Sep 26, 2024

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].

  • 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")

@aifirstd3v
Copy link

The following is written by my GPT4o :)

Error Analysis

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.

  • Error explanation:
    • 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.
    • This issue is specific to using MSW in a Next.js server environment. Next.js operates in both browser and server environments, and in server environments, Node.js modules can be used. However, webpack does not automatically handle these server-side modules unless explicitly configured.

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:

  1. if (isServer):

    • This condition ensures that the configuration is only applied in the server environment. Since Next.js operates in both the browser and server environments, this setting solves issues that arise only on the server side.
  2. config.externals = [...(config.externals || []), '_http_common']:

    • This part adds the "_http_common" module to externals, meaning webpack will treat it as an external module and not include it in the bundle.
    • Modules added to externals are not bundled but are instead handled by Node.js at runtime.
  3. config.target = 'node':

    • Setting webpack's target to 'node' ensures that the bundle is optimized for execution in Node.js environments.
    • This is applied only in the server environment, allowing the Next.js server to handle Node.js modules.

Summary of the Solution

  • The reason _http_common could not be found is that webpack does not automatically handle this module.
  • To fix this, _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.

@kettanaito
Copy link
Member

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 canary if you don't want to wait.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node
Projects
None yet
Development

No branches or pull requests

6 participants