SyntaxError: Unexpected string in msw

751 Views Asked by At

Recently i started getting this error for mock service worker (msw) in my react project.

Details:

  • Node v16.19.1
  • vite/3.2.6 linux-x64 node-v16.19.1
  • react 18.2.0

Checked google, github issues but no good.

enter image description here

enter image description here

1

There are 1 best solutions below

3
kettanaito On

This error is caused by the graphql package releasing a build that cannot run in the browser. Since MSW depends on graphql, the error surfaces for you when you try to run MSW in the browser.

The issue has been resolved in the graphql package, although I've heard that it has regressed since then and you have to pin the graphql version in order for it to remain fixed. [email protected] seems to be fixing this issue.

You can use overrides with NPM (and resolutions with Yarn) to pin that module's version in place:

// package.json
{
  "overrides": {
    "graphql": "16.7.1"
  }
}

Hopefully, this won't regress on the latest GraphQL package and you will be able to use MSW without errors.