【问题标题】:ReferenceError: require is not defined when importing ApolloClient from 'apollo-boost'ReferenceError:从“apollo-boost”导入 ApolloClient 时未定义要求
【发布时间】:2019-04-11 22:25:10
【问题描述】:

所以我收到这个奇怪的错误,ReferenceError: require is not defined 从“apollo-boost”导入 ApolloClient 时。不导入 ApolloClient 应用程序正在运行,但在导入时显示错误。 错误的屏幕截图在所附图像中: Here is the image

这是我的代码

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './components/App';

import  ApolloClient  from 'apollo-boost';
import { ApolloProvider } from 'react-apollo';


const client = new ApolloClient({
    uri:"http://localhost:4444/graphql"
})
ReactDOM.render(
    <ApolloProvider client={client}>
        <App />
    </ApolloProvider>
, document.getElementById('root'));

【问题讨论】:

    标签: reactjs graphql react-apollo graphql-js apollo-client


    【解决方案1】:

    安装 graphql v14.0.2(或新的)。 Graphql 包必须在你的 package.json 中。

    有关更多信息,请参阅此处的原始解决方案:https://github.com/apollographql/apollo-link-state/issues/299#issuecomment-421784547

    【讨论】:

      【解决方案2】:

      转到 node_modules/react-scripts/config/webpack.config(两个 dev && prod 文件)

      {
        test: /\.mjs$/,
        include: /node_modules/,
        type: "javascript/auto"
      },
      

      上面的代码在rules: [里面过去

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-05
        • 2015-06-12
        • 2021-11-08
        • 2018-03-01
        • 1970-01-01
        • 2023-03-10
        • 2019-03-19
        相关资源
        最近更新 更多