msw: Can't resolve '@mswjs/interceptors/lib/interceptors/ClientRequest'
Prerequisites
- I confirm my issue is not in the opened issues
- I confirm the Frequently Asked Questions didn’t contain the answer to my issue
Environment check
- I’m using the latest
mswversion - 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
- clone repo above
- git checkout a3963b2618981992bab115a8e8543abdb9cdc655
- navigate to website folder
- yarn install
- yarn start
- 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
- downgrade msw to avoid ESM bug More details here: https://github.com/mswjs/msw/issues/1267 — committed to octochangelog/octochangelog-webapp by Belco90 2 years ago
- feat: better esm support fixes #1267 — committed to ivanhofer/msw by ivanhofer 2 years ago
- downgrade msw to avoid ESM bug More details here: https://github.com/mswjs/msw/issues/1267 — committed to octochangelog/octochangelog-webapp by Belco90 2 years ago
- downgrade msw to avoid ESM bug More details here: https://github.com/mswjs/msw/issues/1267 — committed to octochangelog/octochangelog-webapp by Belco90 2 years ago
- downgrade msw to avoid ESM bug More details here: https://github.com/mswjs/msw/issues/1267 — committed to octochangelog/octochangelog-webapp by Belco90 2 years ago
- downgrade msw to avoid ESM bug More details here: https://github.com/mswjs/msw/issues/1267 — committed to octochangelog/octochangelog-webapp by Belco90 2 years ago
- downgrade msw to avoid ESM bug More details here: https://github.com/mswjs/msw/issues/1267 — committed to octochangelog/octochangelog-webapp by Belco90 2 years ago
- Setup mocks with MSW (#1148) * introduce ConditionallyRender component * install msw * add env vars for api mocking * init mock service worker * remove unused stuff * remove unnecessar... — committed to octochangelog/octochangelog-webapp by Belco90 2 years ago
- feat: better esm support fixes #1267 — committed to ivanhofer/msw by ivanhofer 2 years ago
- fix: msw 関連のモジュールが解決に失敗する CJSとして読み込み実行することで対処 ref https://github.com/mswjs/msw/issues/1267#1399 ``` error - unhandledRejection: Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/var/home/kimia... — committed to npocccties/chiloportal by knokmki612 2 years ago
- fix: msw 関連のモジュールが `yarn dev` 時解決に失敗する CJSとして読み込み実行することで対処 ref https://github.com/mswjs/msw/issues/1267#1399 ``` error - unhandledRejection: Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/v... — committed to npocccties/chiloportal by knokmki612 2 years ago
- fix: msw 関連のモジュールが `yarn dev` 時解決に失敗する CJSとして読み込み実行することで対処 ref https://github.com/mswjs/msw/issues/1267#1399 ``` error - unhandledRejection: Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/v... — committed to npocccties/chiloportal by knokmki612 2 years ago
- feat: makes the library esm-compatible (#1399) * feat: better esm support fixes #1267 * refactor: add workaround for `useFetch` * refactor: move `statuses` to `devDependencies` * fix: rem... — committed to mswjs/msw by ivanhofer 2 years ago
- Revert "fix: msw 関連のモジュールが `yarn dev` 時解決に失敗する" This reverts commit 68e03ee817ef3d6181939ae094a0f15c10573469. https://github.com/mswjs/msw/issues/1267#1399 解決済みのため — committed to npocccties/chiloportal by knokmki612 7 months ago
https://github.com/mswjs/msw/issues/1267#issuecomment-1297776977
FYI the issue with
bufferUtilsstill occurs in0.48.0Workaround: downgrade to
0.46.1I 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
bufferUtilserror after upgrading to0.48.0but it’s now working for me after upgrading to 1.2.1just 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.
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
moduleimport frommsw/nodedirectly, 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