graphql-js: Regression: Importing the package fails in a brand new Vite project

Minimal repro: https://github.com/srmagura/graphql-repro

The project was created with:

npm create-vite@latest graphql-repro

Then I chose “Vanilla” and “TypeScript” at the prompts.

Install the graphql npm package and add the import

import * as graphql from "graphql";

It produces an error like:

Uncaught SyntaxError: Unexpected string (at graphql.js?v=4b27e75c:1248:113)

If you click on the error in the console, you’ll see that process.env.NODE_ENV has been replaced with “development”.

image

Let me know if you think this is a Vite issue instead, and I will report it there.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 19
  • Comments: 16 (1 by maintainers)

Most upvoted comments

@srmagura It’s an issue with Vite internals, but I added a workaround for it in 16.7.1 Can you please confirm that it is fixed?

Just adding more info - the $ in _globalThis$process seen here is what’s not accounted for in the regexp - https://diff.intrinsic.com/graphql/16.6.0/16.7.0#file-jsutils__instanceOf.mjs

So _globalThis$process.env.NODE_ENV gets changed to _globalThis$"development" by Vite

This got us too – I think the regexp here is not working quite right: https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/clientInjections.ts#L88-L95 . I think this is a bonified vite bug but because both libraries are so popular this error is going to cause a lot of pain. Worth yanking, or changing the transpilation to work fine with vite out of the box?