-
-
Notifications
You must be signed in to change notification settings - Fork 525
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
Package path ./browser is not exported from package (Next.js) #1877
Comments
im having the same issue and cannot migrate because of it |
fixed by changing the import from ES Module to CommonJS |
I'm having the same issue. It's easy to reproduce:
$ npx create-next-app@latest
$ cd my-app
$ npm i -D msw
It makes no sense, because the "exports field in /Users/my.user/my-app/node_modules/msw/package.json" clearly shows:
😖😖 |
Same thing happens when you run dependency-cruiser: |
Same issue here, when trying to access setupWorker from within a dynamically imported module. It works when the import is static but my build process involves some dynamic module loading like: const myModule = await import(jsModulePath) And in turn the module at jsModulePath has a transitive dependency on another module that imports setupWorker. Exact same error message. I can see that the export is indeed defined in package.json. |
My workaround is to create the api mocks as a separate stand-alone module which is all statically linked and then load it in a separate script tag at runtime with the There do seem to be a few issues around package |
This issue appears to be due to NextJS attempting to import if (typeof window !== 'undefined') {
const { setupWorker } = await import('msw/browser');
… |
I found duplicate issue here try this in your
|
I can add that the same problem is present in Nuxt as well. Will try the workarounds in this thread. |
I suspect this error is happening because Next.js runs client-only components in Node.js for SSR. This effectively means that a JavaScript module is evaluated in a Node.js environment. This causes webpack to treat the export conditions as those of "node", which fails "msw/browser" imports, which are completely legal imports in the client. I'm a bit perplexed by this because even if I put a dynamic import into The suggestion from @RaflyLesmana3003 does help but it's rather a hack and I don't recommend it. What it does it tells webpack to ignore some of MSW's exports based on the environment so webpack wouldn't error. Suppressing the error, though, doesn't mean things are working properly. I will close this issue because it cannot be addressed on MSW's side. |
Also keep track of the official Next.js + MSW example (mswjs/examples#101). It's not ready yet but once it is, you will have a reference point. |
For the record, this also happens with Nuxt.js and Vite, in conjunction with Histoire (a Storybook replacement). SSR is disabled in our configuration. This is the only package displaying this problem, so I still figure it may be connected to MSW. I'll post a comment/PR if I can pinpoint exactly how. |
@filiphazardous, it's a combination of MSW using export conditions and whichever compiler you have not respecting those export conditions correctly. In hindsight, export conditions seem to be extremely new and their support differs vastly across tooling. Can you please share a reproduction repository? I would love to have one to look into the problem. |
This worked for me on Next13 import { handlers } from './handlers'
export const setUpMocks = async () => {
if (typeof window !== 'undefined') {
const { setupWorker } = require("msw/browser");
const worker = setupWorker(...handlers)
}
} |
I'm sorry, but we moved on to another solution - this was a show stopper for us. My platform is Vue + Nuxt 3 + Vite 5. (Vite may have been at version 4 when I encountered the problem.) |
I had this same issue in a Docusaurus project (ridiculous, I know, I promise I had a real use case). I was able to solve it with a hack similar to the one listed above (and at #1801), creating a custom Docusaurus plugin in order to modify the webpack config with the following strategy: plugins/webpack-loader/index.js module.exports = function (context, options) {
return {
name: 'webpack-loader',
configureWebpack(config, isServer) {
if (isServer) {
return {
resolve: {alias: {'msw/browser': false}},
};
}
},
};
}; This hacky solution differs from the one above because I found that adding a Leaving this comment in case it helps anyone in a similar situation. |
Works for me with this declaration:
|
This is how I got it in my Next.js app. // helper_functions/setup-msw-browser.js
let mswWorker;
const mockHandlers = [];
export const setupMswWorker = async () => {
if (typeof window !== 'undefined' && !mswWorker) {
const { setupWorker } = await import('msw/browser');
mswWorker = setupWorker(...mockHandlers);
return mswWorker;
}
}; Then in the import { setupMswWorker } from "helper_functions/setup-msw-browser";
import { useEffect } from "react";
import { QueryClient, QueryClientProvider } from "react-query";
const queryClient = new QueryClient();
export default function App({ Component, pageProps }) {
const getLayout = Component.getLayout || (page => page);
useEffect(() => {
let mswWorker;
const startMswWorker = async () => {
mswWorker = await setupMswWorker();
await mswWorker?.start();
}
if (process.env.NODE_ENV === "development") {
startMswWorker();
}
return () => {
mswWorker?.stop();
}
}, []);
return (
<QueryClientProvider client={queryClient}>
{getLayout(<Component {...pageProps} />)}
</QueryClientProvider>
);
} |
where does this live? in the Next js app directory structure... |
The solutions here to me aren't really solutions though. It's only using the browser version of msw.. My goal is to intercept requests made on the server side (within server components in NextJS) so I can tell Cypress to wait for them to resolve before running any assertions. You can't do this when only starting the browser worker and not the node server in msw. |
### Add below code in next.config.mjs file and then run it's working fine. /** @type {import('next').NextConfig} */ export default nextConfig; |
Prerequisites
Environment check
msw
versionBrowsers
Chromium (Chrome, Brave, etc.)
Reproduction repository
https://github.com/xereda/nextjs-msw-example
Reproduction steps
Current behavior
Expected behavior
Service mock enabled.
The text was updated successfully, but these errors were encountered: