Skip to content
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

Error in the browser console when -> import { setupWorker } from 'msw' #1645

Closed
4 tasks done
ftorrens opened this issue Jun 23, 2023 · 13 comments
Closed
4 tasks done
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node

Comments

@ftorrens
Copy link

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 14 or higher

Node.js version

v19.7.0

Reproduction repository

no exists

Reproduction steps

Just import this library
import { setupWorker } from 'msw'

Current behavior

In the chrome console show this error:

msw.js?v=fae9f746:6010 Uncaught SyntaxError: Unexpected string

The error line is:

instanceOf = globalThis.process && globalThis."development" === "production" ? function instanceOf2(value, constructor) {

Expected behavior

Load the library normally

@ftorrens ftorrens added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Jun 23, 2023
@ftorrens
Copy link
Author

ftorrens commented Jun 23, 2023

I was normally using this library in my project and when I update the dependencies it starts giving me this error. I would be very grateful if it could be solved

@oidualc
Copy link

oidualc commented Jun 23, 2023

Duplicate of #1643

@ftorrens
Copy link
Author

ftorrens commented Jun 23, 2023

Thanks for your reply. I am not using the graphql-js library. In any case, I have installed it with version v16.7.1 and the problem persists. Iam missing something to do? Greetings

@oidualc
Copy link

oidualc commented Jun 23, 2023

Unfortunately even if you don't use graphql, it's bundled in the msw library anyways. That line comes from the graphql-js package, and the issue was supposedly fixed in this very version, but if you are still experiencing this issue it might not be fully fixed and should probably be reported directly in the graphql-js repo. Can you share a minimal public repo to replicate the bug?

@ftorrens
Copy link
Author

Hi, I have prepared a small demo modifying my project, from here you can see the error.

1- Clone or download the project

2- run npm i

3- run npm run dev

Demo:
https://github.com/ftorrens/errorMSW

Thanks a lot

@oidualc
Copy link

oidualc commented Jun 24, 2023

Thank you, this has been reported on graphql/graphql-js#3918 and happens because of graphql/graphql-js#3925. There is already a Pull Request pending graphql/graphql-js#3927 that should fix the issue, at this point the only thing to do is to downgrade graphql to v16.6.0 or wait for a patch release.

@ftorrens
Copy link
Author

Thank you very much for your quick response. I will be using version 16.6.0 until the bug is fixed. Very thankful.

@kettanaito
Copy link
Member

Thanks for reporting this!

As stated above, the issue seems to be caused by a transient dependency of MSW—the graphql package. Respectively, it should be addressed there. Meanwhile, pin the version of graphql to 16.6.0 and wait for the fix to be released. Thanks!

@asantTMC
Copy link

asantTMC commented Sep 18, 2023

Thanks for reporting this!

As stated above, the issue seems to be caused by a transient dependency of MSW—the graphql package. Respectively, it should be addressed there. Meanwhile, pin the version of graphql to 16.6.0 and wait for the fix to be released. Thanks!

Installed graphql 16.6.0 but the issue still remains

EDIT: upgraded to 16.7.1 as well as 16.8.0, still getting the same issue

@doox911-opensource
Copy link

The problem is not solved. I tried different versions of graphql - the error persists.

@mattcosta7
Copy link
Contributor

mattcosta7 commented Nov 8, 2023

The problem is not solved. I tried different versions of graphql - the error persists.

graphql/graphql-js#3918 (comment)

this issue lies in graphql-js, and there's not much we can do about it here, but using @rollup/plugin-replace to replace the offending code should work in your vite or rollup config. If you aren't using those, another similar build tool should do the same.

 replace({
      preventAssignment: true,
      "globalThis.process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
    }),

@doox911-opensource
Copy link

The problem is not solved. I tried different versions of graphql - the error persists.

graphql/graphql-js#3918 (comment)

this issue lies in graphql-js, and there's not much we can do about it here, but using @rollup/plugin-replace to replace the offending code should work in your vite config

 replace({
      preventAssignment: true,
      "globalThis.process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
    }),

I use quasar 2 and It didn't help...

/* eslint-env node */
const replace = require('@rollup/plugin-replace')
const { configure } = require('quasar/wrappers')
const path = require('path')

module.exports = configure(function(/* ctx */) {
  return {
    // default settings
    build: {
      // other settings
      plugins: [
        replace({
          preventAssignment: true,
          'globalThis.development': JSON.stringify(process.env.NODE_ENV),
          // or 
         'globalThis.process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
        }),
      ]
    },

  // other settings
  }
})

@mattcosta7
Copy link
Contributor

mattcosta7 commented Nov 8, 2023

The problem is not solved. I tried different versions of graphql - the error persists.

graphql/graphql-js#3918 (comment)
this issue lies in graphql-js, and there's not much we can do about it here, but using @rollup/plugin-replace to replace the offending code should work in your vite config

 replace({
      preventAssignment: true,
      "globalThis.process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
    }),

I use quasar 2 and It didn't help...

/* eslint-env node */
const replace = require('@rollup/plugin-replace')
const { configure } = require('quasar/wrappers')
const path = require('path')

module.exports = configure(function(/* ctx */) {
return {
// default settings
build: {
// other settings
plugins: [
replace({
preventAssignment: true,
'globalThis.development': JSON.stringify(process.env.NODE_ENV),
// or
'globalThis.process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
}),
]
},

// other settings
}
})

I've never heard of quasar, but a quick search of issues in quasar shows

build: {
  rawDefine: {
    'globalThis.process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
  }
}

..or

build: {
      extendViteConf(viteConf) {
        viteConf.define = {
          'globalThis.process.env.NODE_ENV': JSON.stringify(
            process.env.NODE_ENV
          ),
          'process.env.NODE_ENV': JSON.stringify(
            process.env.NODE_ENV
          ),
        };
      },
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node
Projects
None yet
Development

No branches or pull requests

6 participants