找不到模块:无法解析“apollo-link-context”-REACT,APOLLO-SERVER
Posted
技术标签:
【中文标题】找不到模块:无法解析“apollo-link-context”-REACT,APOLLO-SERVER【英文标题】:Module not found: Can't resolve 'apollo-link-context' - REACT, APOLLO-SERVER 【发布时间】:2020-05-20 04:30:13 【问题描述】:我有一个想要在我的前端文件夹中显示的用户的后端。 为此,我需要通过 Apollo Server 在 React 中实现授权标头。
我遵循了这里的一些指示 -> https://www.apollographql.com/docs/react/networking/authentication/
这是我在 App.js 中的代码
import React from 'react';
import HeroesDota from './components/HeroesDota';
import ApolloProvider from '@apollo/react-hooks';
import ApolloClient from "apollo-client";
import InMemoryCache from "apollo-cache-inmemory";
import HttpLink from "apollo-link-http";
import setContext from 'apollo-link-context';
const httpLink = new HttpLink(
uri: "http://localhost:8000/graphql/",
);
const client = new ApolloClient(
link: authLink.concat(httpLink),
cache: new InMemoryCache()
);
const authLink = setContext((_, headers ) =>
// get the authentication token from local storage if it exists
const token = localStorage.getItem('token');
// return the headers to the context so httpLink can read them
return
headers:
...headers,
authorization: token ? `Bearer $token` : "",
);
const App = () =>
return (
<ApolloProvider client=client>
<HeroesDota />
</ApolloProvider>
);
export default App;
我在 localhost:3000 上运行服务器后,它说:未捕获的错误:找不到模块'apollo-link-context'
我安装了 apollo-link 作为依赖项。我不知道为什么会出现这个错误。这是实际图片。
package.json
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies":
"@apollo/react-hooks": "^3.1.3",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.0",
"@testing-library/user-event": "^7.2.1",
"apollo-boost": "^0.4.7",
"apollo-cache-inmemory": "^1.6.5",
"apollo-link": "^1.2.13",
"apollo-link-ws": "^1.0.19",
"bootstrap": "^4.4.1",
"graphql": "^14.5.8",
"react": "^16.12.0",
"react-apollo": "^3.1.3",
"react-bootstrap": "^1.0.0-beta.16",
"react-dom": "^16.12.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.0",
"styled-components": "^5.0.0",
"subscriptions-transport-ws": "^0.9.16"
,
"scripts":
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
,
"eslintConfig":
"extends": "react-app"
,
"browserslist":
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
,
"devDependencies":
【问题讨论】:
【参考方案1】:你正在这里导入它:
import setContext from 'apollo-link-context';
请运行npm i apollo-link-context
进行修复。
【讨论】:
请分享你的 package.json 我不能。请运行npm i apollo-link-context
。您需要安装apollo-link-context
而不仅仅是apollo-link
。以上是关于找不到模块:无法解析“apollo-link-context”-REACT,APOLLO-SERVER的主要内容,如果未能解决你的问题,请参考以下文章
React 编译失败。找不到模块:无法解析“Main.css”