【问题标题】:ReferenceError: process is not defined Module.../../../node_modules/graphql/jsutils/instanceOf.mjsReferenceError:进程未定义模块.../../../node_modules/graphql/jsutils/instanceOf.mjs
【发布时间】:2021-06-26 09:04:47
【问题描述】:

我正在为我的项目使用 graphql 订阅,它是一个 instagram 克隆,我正在执行喜欢或不喜欢帖子的功能,我使用 npx create-react-app 创建我的客户端,我使用 express 创建我的服务器。

在客户端,我这样设置我的client:

src/apis/client.js

import {
  ApolloClient,
  ApolloLink,
  HttpLink,
  InMemoryCache,
  split,
} from 'apollo-boost';
import {getMainDefinition} from '@apollo/client/utilities';
import {WebSocketLink} from '@apollo/client/link/ws';
const httpUrl = 'http://localhost:5000/graphql';
const wsUrl = 'ws://localhost:5000/graphql';

const httpLink = ApolloLink.from([
  new ApolloLink((operation, forward) => {}),
  new HttpLink({uri: httpUrl}),
]);

const wsLink = new WebSocketLink({
  uri: wsUrl,
  options: {
    // connectionParams: () => {},
    lazy: true,
    reconnect: true,
  },
});

function isSubscription(operation) {
  const definition = getMainDefinition(operation.query);
  return (
    definition.kind === 'OperationDefinition' &&
    definition.operation === 'subscription'
  );
}

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: split(isSubscription, wsLink, httpLink),
  defaultOptions: {query: {fetchPolicy: 'no-cache'}},
});

export default client;

如您所见,我同时使用 websocket 和 https 连接。以前,我只使用http连接,一切正常。

这是我的server 侧:

app.js

const {ApolloServer, gql} = require('apollo-server-express');
const http = require('http');
const fs = require('fs');
const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express');

const app = express();
app.use(cors(), bodyParser.json());

const typeDefs = gql(fs.readFileSync('./schema.graphql', {encoding: 'utf8'}));

const resolvers = require('./resolvers');

const apolloServer = new ApolloServer({
  typeDefs,
  resolvers,
});
apolloServer.applyMiddleware({app, path: '/graphql'});

const httpServer = http.createServer(app);
apolloServer.installSubscriptionHandlers(httpServer);
const port = 5000;
httpServer.listen(port, () => console.log(`Server started on port ${port}`));

这是错误:

我尝试谷歌,但它看起来像 React 的 webpack.config 文件中缺少某些东西,但我不知道 webpack。请告诉我如何处理它,非常感谢,祝你有美好的一天

【问题讨论】:

    标签: javascript express web websocket graphql


    【解决方案1】:

    我遇到了同样的问题,并且能够通过升级 package.json 中的 graphql 依赖项的版本来解决它。我的项目使用的是非常旧版本的 graphql,即“0.13.2”,我将版本更新为“14.6.0”。

    【讨论】:

    • 我仍然面临同样的问题。即使在升级之后。
    • 在任何情况下,此问题都与依赖项的安装有关。您可能需要检查依赖项的版本。如果您将 apollo 与 graphql 一起使用,那么您可以尝试此处发布的解决方案:stackoverflow.com/questions/54861251/… 如果没有任何效果,您可以分享您的 package.json 文件吗?
    【解决方案2】:

    安装特定软件包的最新版本后,我遇到了同样的问题。能够通过删除 node_modules 和 package-lock.json 并再次运行 npm install 来解决问题。

    【讨论】:

    • 这对我不起作用 :( 哪个包是问题?
    • 不记得了。您能否显示您的错误消息的屏幕截图?
    • 我通过 npm install apollo-boost 修复了它,不知何故从我的安装列表中掉了出来
    猜你喜欢
    • 2021-01-06
    • 1970-01-01
    • 2022-08-14
    • 2023-03-07
    • 2023-01-18
    • 2019-04-15
    • 2016-02-28
    • 2013-07-22
    • 1970-01-01
    相关资源
    最近更新 更多