-
-
Notifications
You must be signed in to change notification settings - Fork 516
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
Can't resolve '@mswjs/interceptors/lib/interceptors/ClientRequest' #1267
Comments
Hey, @ntucker. Thanks for reporting this.
That's an interesting part: MSW doesn't publish an ESM version as of now. The Can you try this on |
node_modules/msw/lib/node/index.mjs is commonjs? Mjs extensions should only be used for esm |
node_modules/msw/lib/node/index.mjs is commonest? Mjs extensions should only be used for esmwhat is #1247 about then? It adds mjs extensions |
Oh, you're right, we do publish ESM but only for Line 40 in 1b9dde0
🤦 I'm tried of dancing around with builds. We should remove all ESM build targets. The Interceptors library is (always was) CommonJS. So if the parent module ( I have a suspicion that ESM mean different things in different bundles. We used to have ESM build with Rollup for years but that shouldn't have worked (I've described why above). So confused. |
I believe importing is fine... you just have to specify the file extension anytime you're in an esm. |
@ntucker, yeah, that's the problem: how to do that in an ambiguous context? The source code compiles to multiple targets, so I don't see specifying file extensions all the time as a sensible strategy. Moreover, it raises a compilation error in TS, which forces you not to include extensions. It seems like this should be done by the bundler automatically since it's the bundle that's aware of the build target (ESM) and should provide all the necessary transformations for the source code to produce a valid target code. I'm looking for help with the proper library bundling, so anybody experienced with this is welcome to join the discussion or open a pull request. Thanks. |
Ts doesn't force you to not use extensions. If you add .js it will still look for .d.ts etc. See my project for example bundling: https://github.com/coinbase/rest-hooks |
For instance, you can see in https://github.com/coinbase/rest-hooks/blob/master/packages/core/src/index.ts all the relative imports specify .js in the extension. If you use tsc for compiling your js: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#ecmascript-module-support-in-node-js If you use babel - you can see an example of how I do it https://github.com/coinbase/rest-hooks/blob/master/packages/core/package.json#L55 with and config https://github.com/coinbase/rest-hooks/blob/master/babel.config.js (using @anansi/babel-preset) |
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 |
simplest way to reproduce it is 👉 import { setupServer } from 'msw/node'; execute in the console node test.mjs will result in
Is there a plan to go to esm? or to support esm entrypoints? See node docs for conditional entry points for esm & cjs https://nodejs.org/api/packages.html#packages_conditional_exports |
Yes, there is a plan. Contributions are extremely welcome to make this plan a reality. This particular support doesn't interest me at the moment so I'm unlikely to spend time on it. But I'm always open to providing code reviews and making this support happen. |
I would personally be happy with ESM targets being removed until properly impelemented |
just started having this issue using [email protected] + [email protected] + [email protected]. Is there a workaround til this is fixed but scripting the |
Workaround: downgrade to |
where did you start noticing the issue on the in
make the default the then restart and see if that allows a build to succeed. if so, we might consider dropping the esm node build for now, until we can handle that directly |
|
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 @ivanhofer curious if you've seen that working on your end properly |
It is working for me, because I'm using it in a |
More details here: mswjs/msw#1267
@mattcosta7 I'm also only experiencing this problem since that upgrade (see the build error) and just tried your triage steps in my project. I can confirm that after those steps, the build completed successfully, so it might indeed be worth dropping esm from the Node build for now. (Alternatively, only using default exports when importing from CJS modules in an ES module might also fix actual ES module support.) |
I have investigated this issue a bit. There are a few road blocks to make
|
More details here: mswjs/msw#1267
More details here: mswjs/msw#1267
More details here: mswjs/msw#1267
More details here: mswjs/msw#1267
More details here: mswjs/msw#1267
* introduce ConditionallyRender component * install msw * add env vars for api mocking * init mock service worker * remove unused stuff * remove unnecessary cookie option * add a handler for testing library repo search * add a renovate results for repo search handler * improve chained repos search results * throw error on unhandled requests to github api * simplify fixtures search import * basic repo releases mocking for dom-testing-library * update msw to latest * disable next lint during building * add tests for paginateList util * downgrade eslint to prevent issue in WebStorm ESLint 8.23.0 causes an issue in WebStorm https://youtrack.jetbrains.com/issue/WEB-57089/ESLint823-TypeError-thislibOptionsparse-is-not-a-function Should be solved in WebStorm 2022.2.2 * improve paginateList tests * paginate github repo releases * add extra test for paginateList * update msw to latest * mock repos details request with MSW * add renovate fixtures to msw handlers * enable api mocking by default * adapt Cypress tests to MSW mocks * improve GitHub API host comparison in MSW * Docs improvements from PR suggestions Co-authored-by: HonkingGoose <[email protected]> * msw setup refactor * downgrade msw to avoid ESM bug More details here: mswjs/msw#1267 * update cypress to v10.7.0 * update msw to latest * downgrade msw to avoid ESM bug More details here: mswjs/msw#1267 * remove unnecessary cookie mock for cypress e2e * remove unnecessary local env var for e2e in CI * refactor ignored hosts * refactor githubReposDetailsHandlers * mock network error on Cypress e2e properly * extract msw hook * extract waitForApiMocking Cypress command * supress happotask type errors * more CONTRIBUTING suggestions Co-authored-by: HonkingGoose <[email protected]> * Update CONTRIBUTING.md Co-authored-by: HonkingGoose <[email protected]> * more text nitpicks Co-authored-by: HonkingGoose <[email protected]> * update cypress to v10.8.0 * pass e2e flag to e2e commands * increase timeout for test case when loading more than 10 pages * regenerate lock Co-authored-by: HonkingGoose <[email protected]>
CJSとして読み込み実行することで対処 ref mswjs/msw#1267 (comment) ``` error - unhandledRejection: Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/var/home/kimiaki/Workspace/github.com/npocccties/chiloportal/frontend/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest' is not supported resolving ES modules imported from /var/home/kimiaki/Workspace/github.com/npocccties/chiloportal/frontend/node_modules/msw/lib/node/index.mjs Did you mean to import @mswjs/interceptors/lib/interceptors/ClientRequest/index.js? at new NodeError (node:internal/errors:387:5) at finalizeResolution (node:internal/modules/esm/resolve:425:17) at moduleResolve (node:internal/modules/esm/resolve:1006:10) at defaultResolve (node:internal/modules/esm/resolve:1214:11) at nextResolve (node:internal/modules/esm/loader:165:28) at ESMLoader.resolve (node:internal/modules/esm/loader:844:30) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:431:18) at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40) at link (node:internal/modules/esm/module_job:75:36) { code: 'ERR_UNSUPPORTED_DIR_IMPORT', url: 'file:https:///var/home/kimiaki/Workspace/github.com/npocccties/chiloportal/frontend/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest' } ```
CJSとして読み込み実行することで対処 ref mswjs/msw#1267 (comment) ``` error - unhandledRejection: Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/var/home/kimiaki/Workspace/github.com/npocccties/chiloportal/frontend/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest' is not supported resolving ES modules imported from /var/home/kimiaki/Workspace/github.com/npocccties/chiloportal/frontend/node_modules/msw/lib/node/index.mjs Did you mean to import @mswjs/interceptors/lib/interceptors/ClientRequest/index.js? at new NodeError (node:internal/errors:387:5) at finalizeResolution (node:internal/modules/esm/resolve:425:17) at moduleResolve (node:internal/modules/esm/resolve:1006:10) at defaultResolve (node:internal/modules/esm/resolve:1214:11) at nextResolve (node:internal/modules/esm/loader:165:28) at ESMLoader.resolve (node:internal/modules/esm/loader:844:30) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:431:18) at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40) at link (node:internal/modules/esm/module_job:75:36) { code: 'ERR_UNSUPPORTED_DIR_IMPORT', url: 'file:https:///var/home/kimiaki/Workspace/github.com/npocccties/chiloportal/frontend/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest' } ```
CJSとして読み込み実行することで対処 ref mswjs/msw#1267 (comment) ``` error - unhandledRejection: Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/var/home/kimiaki/Workspace/github.com/npocccties/chiloportal/frontend/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest' is not supported resolving ES modules imported from /var/home/kimiaki/Workspace/github.com/npocccties/chiloportal/frontend/node_modules/msw/lib/node/index.mjs Did you mean to import @mswjs/interceptors/lib/interceptors/ClientRequest/index.js? at new NodeError (node:internal/errors:387:5) at finalizeResolution (node:internal/modules/esm/resolve:425:17) at moduleResolve (node:internal/modules/esm/resolve:1006:10) at defaultResolve (node:internal/modules/esm/resolve:1214:11) at nextResolve (node:internal/modules/esm/loader:165:28) at ESMLoader.resolve (node:internal/modules/esm/loader:844:30) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:431:18) at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40) at link (node:internal/modules/esm/module_job:75:36) { code: 'ERR_UNSUPPORTED_DIR_IMPORT', url: 'file:https:///var/home/kimiaki/Workspace/github.com/npocccties/chiloportal/frontend/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest' } ```
White we are waiting for the PR to be merged, here is my temporary fix with Export of the msw-npm-0.47.4-c11a8d2944.patch file
|
@turboninh Can you please export the commit you patched and post it here? |
@morrisonbrett Updated my comment :) |
* feat: better esm support fixes #1267 * refactor: add workaround for `useFetch` * refactor: move `statuses` to `devDependencies` * fix: remove unneeded type assertion again Co-authored-by: Artem Zakharchenko <[email protected]>
Released: v0.48.0 🎉This has been released in v0.48.0! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
FYI the issue with
|
Me too.. FYI the issue with bufferUtils still occurs in ^0.48.0
|
I wonder if the error on 0.48.0 is still there because of the interceptors? Looking at this comment #1399 (review) |
I was getting the same |
This reverts commit 68e03ee. mswjs/msw#1267 (comment) 解決済みのため
Prerequisites
Environment check
msw
versionBrowsers
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
Current behavior
#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'
The text was updated successfully, but these errors were encountered: