msw: Can't resolve '@mswjs/interceptors/lib/interceptors/ClientRequest'

Prerequisites

Environment check

  • I’m using the latest msw version
  • I’m using Node.js version 14 or higher

Browsers

NODE 16/18

Reproduction repository

https://github.com/coinbase/rest-hooks/tree/a3963b2618981992bab115a8e8543abdb9cdc655/website

Gonna revert MSW upgrade to make this work so master will no longer have this problem.

Reproduction steps

  1. clone repo above
  2. git checkout a3963b2618981992bab115a8e8543abdb9cdc655
  3. navigate to website folder
  4. yarn install
  5. yarn start
  6. observe error in cli as well as opened browser tab

Current behavior

ERROR in ./node_modules/msw/lib/node/index.mjs 45:0-94

Module not found: Error: Can't resolve '@mswjs/interceptors/lib/interceptors/ClientRequest' in '/home/ntucker/src/rest-hooks/website/node_modules/msw/lib/node'
Did you mean 'index.js'?
BREAKING CHANGE: The request '@mswjs/interceptors/lib/interceptors/ClientRequest' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

https://github.com/mswjs/msw/pull/1247 breaks in node versions that support ESM

Expected behavior

To fix this - the ESM version of MSW must be explicit about file extensions. Otherwise it is by definition incorrectly built. If this cannot be done you should not ship a custom ESM version as this is incorrect. The only reason it would work is if it is used by a system that doesn’t enforce fully (webpack). However this will break in fully-spec compliant things like node.

More simply:

import '@mswjs/interceptors/lib/interceptors/ClientRequest' => import '@mswjs/interceptors/lib/interceptors/ClientRequest.mjs'

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 35
  • Comments: 29 (11 by maintainers)

Commits related to this issue

Most upvoted comments

https://github.com/mswjs/msw/issues/1267#issuecomment-1297776977

FYI the issue with bufferUtils still occurs in 0.48.0

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'node_modules/@mswjs/interceptors/lib/utils/bufferUtils' imported from node_modules/msw/lib/node/index.mjs
Did you mean to import @mswjs/interceptors/lib/utils/bufferUtils.js

Workaround: downgrade to 0.46.1

I would personally be happy with ESM targets being removed until properly impelemented

Again, this implementation is against spec so it will break in all environments that implement spec correctly: https://nodejs.org/api/esm.html#mandatory-file-extensions

I was getting the same bufferUtils error after upgrading to 0.48.0 but it’s now working for me after upgrading to 1.2.1

just started having this issue using msw@0.47.0 + ts@4.8.2 + next@12.2.5.

Is there a workaround til this is fixed but scripting the node_modules?

@morrisonbrett Updated my comment 😃

@turboninh Can you please export the commit you patched and post it here?

I have created a draft PR that fixes point nr. 1. If anyone knows a solution to the other points. Feel free to contribute.

just started having this issue using msw@0.47.0 + ts@4.8.2 + next@12.2.5. Is there a workaround til this is fixed but scripting the node_modules?

where did you start noticing the issue on the msw/node side or the msw direct side?

msw/node. In the client it was working fine but the server part was broken

I added a few triage steps to my last message, if you wouldn’t mind giving them a shot, and describing output. it might help with getting that fixed a bit

I think we probably never used the module import from msw/node directly, since the package.json wasn’t type: module there, and in https://github.com/mswjs/msw/pull/1383/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519R15-R16 that might not be compat.

@ivanhofer curious if you’ve seen that working on your end properly