How to use MSW with Parcel? #557
-
I have a small TypeScript React project that I bundle using Parcel. I wanted to add MSW for mocking server requests, but I have troubles getting it to work. I have a very minimalistic Parcel setup: "scripts": {
"start": "parcel index.html",
"build": "parcel build index.html"
}, And as for the MSW, everything is done according to the documentation. Here's how the service-worker is initiated: if (process.env.NODE_ENV === "development") {
const { worker } = require("./mocks/browser")
worker.start()
} The same exact implementation works perfectly when bundling with Webpack. But in Parcel it results in following errors in the console:
Is there a way to get it to work? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hey, @zorzysty. Have you completed the browser integration steps? The error that's being shown means that there's no
I believe if you achieve the |
Beta Was this translation helpful? Give feedback.
Hey, @zorzysty.
Have you completed the browser integration steps?
The error that's being shown means that there's no
mockServiceWorker.js
file served by your app. This can happen because of the following reasons:npx msw init <PUBLIC_DIR>
(see the docs page above for more info).PUBLIC_DIR
path, so the worker is not where MSW expects it to be. You can easily check that: this file should exist:http://localhost:XXXX/mockServiceWorker.js
, whereXXXX
is the port where your app is running locally.I believe if you achieve the
/mockServiceWorker.js
file being served by the roo…