Node modules broken When using Vite + React + Apollo Client

huangapple go评论67阅读模式
英文:

Node modules broken When using Vite + React + Apollo Client

问题

我尝试使用@apollo/client,参考这个起始 Apollo 客户端文档,但想要在 CRA(Create React App)之外使用 Vite。我在 main.jsx 中导入了模块,如下所示:

import {
  ApolloClient,
  InMemoryCache,
  ApolloProvider,
  gql,
} from "@apollo/client/core";

const client = new ApolloClient({
  uri: "https://flyby-router-demo.herokuapp.com/",
  cache: new InMemoryCache(),
});

// ...

ReactDOM.createRoot(document.getElementById("root")).render(
  <ApolloProvider client={client}>
    <React.StrictMode>
      <App />
    </React.StrictMode>
  </ApolloProvider>
);

然后我的应用程序报错并显示以下消息:

Uncaught SyntaxError: missing ) in parenthetical chunk-NWQ2EI35.js:1493:112

在 "chunk-NWQ2EI35.js:1493:112" 中,我可以看到以下 JavaScript 代码:

// node_modules/graphql/jsutils/instanceOf.mjs
var _globalThis$process;
var instanceOf = (
  /* c8 ignore next 6 */
  // FIXME: https://github.com/graphql/graphql-js/issues/2317

  // !!! 这是第1493行 !!!
  ((_globalThis$process = globalThis.process) === null || _globalThis$process === void 0 ? void 0 : _globalThis$&quot;development&quot;) === &quot;production&quot; ? function instanceOf2(value, constructor) { 

  // ...

源自 node_modules/graphql/jsutils/instanceOf.js 的代码如下:

const instanceOf =
  /* c8 ignore next 6 */
  // FIXME: https://github.com/graphql/graphql-js/issues/2317
  ((_globalThis$process = globalThis.process) === null ||
  _globalThis$process === void 0
    ? void 0
    : _globalThis$process.env.NODE_ENV) === 'production'
    ? function instanceOf(value, constructor) {
        return value instanceof constructor;
      }
    : function instanceOf(value, constructor) {
        if (value instanceof constructor) {
          return true;
        }

    // ...

看起来 _globalThis$process.env.NODE_ENV 已更改为 _globalThis$&quot;development&quot;,这导致了问题。

根据您的经验,我不知道如何在 Vite 环境中使用 Apollo 客户端。需要一些帮助。

英文:

I tried @apollo/client with this starting apollo client document but wanted to use Vite instead of CRA. I imported the modules in main.jsx like this:

import {
  ApolloClient,
  InMemoryCache,
  ApolloProvider,
  gql,
} from &quot;@apollo/client/core&quot;;

const client = new ApolloClient({
  uri: &quot;https://flyby-router-demo.herokuapp.com/&quot;,
  cache: new InMemoryCache(),
});

// ...

ReactDOM.createRoot(document.getElementById(&quot;root&quot;)).render(
  &lt;ApolloProvider client={client}&gt;
    &lt;React.StrictMode&gt;
      &lt;App /&gt;
    &lt;/React.StrictMode&gt;
  &lt;/ApolloProvider&gt;
);

and then my app crushed with this message

Uncaught SyntaxError: missing ) in parenthetical chunk-NWQ2EI35.js:1493:112

In "chunk-NWQ2EI35.js:1493:112" I could see this js code.

// node_modules/graphql/jsutils/instanceOf.mjs
var _globalThis$process;
var instanceOf = (
  /* c8 ignore next 6 */
  // FIXME: https://github.com/graphql/graphql-js/issues/2317

  // !!! THIS is the line #1493 !!!
  ((_globalThis$process = globalThis.process) === null || _globalThis$process === void 0 ? void 0 : _globalThis$&quot;development&quot;) === &quot;production&quot; ? function instanceOf2(value, constructor) { 

  // ...

Source from node_modules/graphql/jsutils/instanceOf.js :

const instanceOf =
  /* c8 ignore next 6 */
  // FIXME: https://github.com/graphql/graphql-js/issues/2317
  ((_globalThis$process = globalThis.process) === null ||
  _globalThis$process === void 0
    ? void 0
    : _globalThis$process.env.NODE_ENV) === &#39;production&#39;
    ? function instanceOf(value, constructor) {
        return value instanceof constructor;
      }
    : function instanceOf(value, constructor) {
        if (value instanceof constructor) {
          return true;
        }

    // ...

Seems _globalThis$process.env.NODE_ENV changed to _globalThis$&quot;development&quot;, and this caused the problem.

I have no idea how to use apollo client with vite environment. Needed some helps based on your experiences.

答案1

得分: 3

这是由昨天的graphql版本更新引起的。正在进行新的发布以修复此问题,与此同时,您可以将graphql降级到16.6.0

编辑:现在应该已经通过16.7.1修复了此问题。

英文:

This is caused by a graphql version update from yesterday. A new release to fix this is in the works, meanwhile you can downgrade graphql to 16.6.0.


Edit: this should be fixed with 16.7.1 now.

答案2

得分: 3

在我的项目中出现了相同的问题(Nuxt 3,Cloudflare Pages 预设),即使使用 graphql 版本 16.7.1 也是如此。

不需要降级,它已在 17.0.0-alpha.2 中修复。

将以下内容添加到您的 package.json

"overrides": {
  "graphql": "^17.0.0-alpha.2"
}
英文:

The same issue appeared in my project (Nuxt 3, Cloudflare Pages preset), even with graphql version 16.7.1.

Not need to downgrade, it is fixed in 17.0.0-alpha.2.

Add this to your package.json:

  &quot;overrides&quot;: {
    &quot;graphql&quot;: &quot;^17.0.0-alpha.2&quot;
  }

huangapple
  • 本文由 发表于 2023年6月22日 17:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定