Skip to content

ESM: import { request } from 'node:https' bypasses interception until the patched https.request is called once #2824

Open

Description

@AndrewBoklashko

Prerequisites

Environment check

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

Node.js version

v24.15.0

Reproduction repository

https://github.com/AndrewBoklashko/msw-esm-named-import-repro

Reproduction steps

npm install
npm run repro       # node repro.mjs
npm run workaround  # node repro.mjs --sync (calls module.syncBuiltinESMExports() after server.listen())

Current behavior

In ES module, request imported by name from node:https isn't intercepted: the request goes to the network.

1. named import          : ENOTFOUND
2. default https.request : {"intercepted":true}
3. named import again    : {"intercepted":true}

It's order-dependent: once the patched https.request (default import) has been called, later named-import calls are intercepted too, so the first call through a named import is the one that leaks.

Real-world case: aws-jwt-verify ESM build does import { request } from "https" to fetch JWKS, so a test that mocks the JWKS endpoint fails until something else makes a request first.

Named ESM imports of builtins are bound to a separate copy of the module's exports, which Node only refreshes on module.syncBuiltinESMExports(). Patching https.request on the CommonJS exports doesn't reach it. Same report: mswjs/interceptors#201 (closed, unresolved).

Expected behavior

Named imports are intercepted from the first request, like the default import.

Calling syncBuiltinESMExports() from node:module right after applying the patches (and again after restoring them) fixes it: with --sync, all three requests are intercepted. Could the interceptor do that itself?

Activity

  1. added
    bug
    scope:node
    needs:triage
    on Oct 26, 2026
  2. Hugo510 commented on Nov 3, 2026

    @Hugo510

    Hi! I reproduced this on current mswjs/interceptors main (31cc4c3b, 0.45.11) with Node 22.20.0 on Windows.

    It affects the first call through native ESM named imports of all four HTTP functions: http.request, http.get, https.request, and https.get. My tests use a local HTTP/HTTPS server: without the change, they receive the real server's response instead of the mocked response. The fixture is loaded by Node through createRequire, so Vitest's import transformation cannot hide the stale bindings.

    I have a local change in ClientRequestInterceptor that calls syncBuiltinESMExports() after installing the patches and again after restoring them during disposal. Five focused tests pass (four request cases plus binding restoration). A separate native Node script also verifies two apply/dispose cycles and successful real requests after disposal.

    Validation: 267 HTTP tests passed, 3 skipped; 26 ClientRequest unit tests passed; TypeScript and the Node build passed. Three other HTTP test files fail on this Windows setup with and without the change (Unix socket setup, a child-process launch, and a multipart passthrough ECONNRESET).

    Would you welcome this regression test and fix as a PR in mswjs/interceptors, linked to this issue?

    Investigation, tests, fix, and this comment were prepared with OpenAI Codex assistance. The checks above were executed locally.

  3. AndrewBoklashko commented on Nov 21, 2026

    @AndrewBoklashko
    Author

    Hi MSW team
    Any feedback on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds:triagescope:node

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions