-
-
Notifications
You must be signed in to change notification settings - Fork 529
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
Next.js Project Fails to Compile due to Package path ./browser is not exported from package /node_modules/msw
#1801
Comments
I think your msw/browser is called in server environment;
If I ensure mocks/browser.ts is imported in client (typeof window !== 'undefined'), it works. |
Following this migration guide: https://mswjs.io/docs/migrations/1.x-to-2.x Note that the migration is not yet complete; we're still running into what might be an MSW bug, described in mswjs/msw#1801.
Following this migration guide: https://mswjs.io/docs/migrations/1.x-to-2.x Note that the migration is not yet complete; we're still running into what might be an MSW bug, described in mswjs/msw#1801.
Following this migration guide: https://mswjs.io/docs/migrations/1.x-to-2.x Note that the migration is not yet complete; we're still running into what might be an MSW bug, described in mswjs/msw#1801.
Same here, but getting following output error instead: Module not found: Package path ./node is not exported from package /usr/app/node_modules/msw
(see exports field in /usr/app/node_modules/msw/package.json) Maybe it is worth noting that, in my case, the error is triggered while trying to access a file packed in a third-party library which has been migrated to the latest msw version as well. Somehow, I think that following import (in the third party library): import { setupServer } from 'msw/node'; did not get correctly exported. |
We had the same issue in our Next.js app. I our case it's caused by the fact that we have our package with complete network toolkit including msw. In the package we have following snippet (simplified for illustration) // index.ts
export * from "./jestHelpers" // includes `import {} from msw/node`
export * from "./browserHelpers" // includes `import {} from msw/browser`
export * from "./nodeHelpers" // includes `import {} from msw/node` The error happens during build time (not runtime). None of But that's a good thing (I think) because it serves as strict check that node code isn't included in browser bundle and vice versa. Having the code there could cause deeper and hard to debug problems. Which we did have in past with msw@1 We've workaround this, or better say enforced it on our side using aliases in webpack (docs on how to add custom webpack config) and with that the issue goes away. So for anyone who has the same problem this is how you can fix it. However it would be good to add to migration guide and/or FAQ with explanation why is it happening. if (context?.isServer) {
// next server build => ignore msw/browser
if (Array.isArray(config.resolve.alias)) { // in Next the type is always object, so this branch isn't necessary. But to keep TS happy, avoid @ts-ignore and prevent possible future breaking changes it's good to have it
config.resolve.alias.push({ name: "msw/browser", alias: false })
} else {
config.resolve.alias["msw/browser"] = false
}
} else {
// browser => ignore msw/node
if (Array.isArray(config.resolve.alias)) {
config.resolve.alias.push({ name: "msw/node", alias: false })
} else {
config.resolve.alias["msw/node"] = false
}
} |
Hi @jakubriedl. Thanks. And yes, this is how (I came across this workaround over the weekend as well) I solved it on our end: // next.config.js
module.exports = {
webpack: (config, { isServer }) => {
if (!isServer) {
// Setting `resolve.alias` to `false` will tell webpack to ignore a module.
// `msw/node` is a server-only module that exports methods not available in
// the `browser`.
config.resolve.alias = {
...config.resolve.alias,
'msw/node': false,
};
}
return config;
},
...
}; I was thinking about doing the same for This looks like a hack to me still. I mean, we are talking about a test dependency which somehow manages to disturb our whole deployment. I am not deep enough in Next.js/webpack to suggest a proper solution here. In my head, all the tests and associated imports (which might be parts of external dependencies) should just be ignored by Next.js during building. So, in the end, this is maybe a Next.js problem? Thanks for confirming my hack anyway... 💪 |
Following this migration guide: https://mswjs.io/docs/migrations/1.x-to-2.x Note that the migration is not yet complete; we're still running into what might be an MSW bug, described in mswjs/msw#1801.
Thank you so much!
|
Thanks for the active participation here, @ribeaud @jakubriedl! I agree the proposed solution may work but is indeed a hack. You shouldn't be meddling with import resolution. Next should do that for you. Export conditions are relatively new in Node.js so I suspect Next has to respect them correctly, loading You are free to follow the proposed solution above until that happens. There's nothing we can or should do on MSW's side to mitigate this. I'm fairly confident our exports are correctly written. We forbid |
In which file do I add this? |
In the webpack entry of your |
I am really confused as to why Next.js doesn't use the export conditions to properly resolve third-party packages (afaik, Rollup supports those). They know the import context ( Also, this can still be the consumer's fault if the |
I'm importing a component with a 'use client' directive, where I import |
I tried this solution but now I'm getting |
Description
I'm working on a Next.js project where I'm using MSW for mocking API calls. However, after updating to msw v2.0.0 I am unable to compile the project due to the following error:
Steps to Reproduce
npm install msw
oryarn add msw
.mocks/browser.ts
file to set up MSW.setupWorker
frommsw/browser
in thebrowser.ts
file.import { setupWorker } from 'msw/browser';
npm run dev
oryarn dev
.Expected Behavior
The Next.js project should compile successfully and MSW should intercept the network requests.
Actual Behavior
The project fails to compile with the error mentioned above.
Additional Information
The text was updated successfully, but these errors were encountered: