学习Apollo Client时遭遇ENOENT文件缺失错误,请求技术支持
optimism/lib/bundle.esm.js in Apollo Client Hey there, let's get this sorted out! The error you're seeing happens because of conflicting or incomplete dependencies in your project—let's break down the solution step by step.
What's causing this?
Your package.json has a mix of old and new Apollo Client dependencies that are clashing:
- You're using
@apollo/clientv3 (which includes all modern features like hooks, cache, and link utilities out of the box) - But you also have outdated packages like
apollo-boost,@apollo/react-hooks(unnecessary for v3), andapollo-clientv2 installed. These conflicting versions mess up your dependency tree, leading to missing files like theoptimismbundle.
Step-by-Step Fix
1. Clean up existing dependencies
First, delete your node_modules folder and lock file to start fresh:
- On Windows:
rmdir /s /q node_modules del package-lock.json - On macOS/Linux:
rm -rf node_modules package-lock.json
2. Simplify your package.json dependencies
Remove all redundant Apollo-related packages. You only need @apollo/client and graphql for a basic setup. Update your dependencies section to look like this:
{ "name": "pokemon", "version": "0.1.0", "private": true, "dependencies": { "@apollo/client": "^3.3.12", "@testing-library/jest-dom": "^5.11.4", "@testing-library/react": "^11.1.0", "@testing-library/user-event": "^12.1.10", "graphql": "^15.5.0", "react": "^17.0.1", "react-dom": "^17.0.1", "react-scripts": "4.0.3", "web-vitals": "^1.0.1" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] } }
3. Reinstall dependencies
Run this command to install the cleaned-up dependencies:
npm install
4. Update your App.js to use Apollo Client v3 correctly
apollo-boost is deprecated for v3—instead, build your client using the utilities from @apollo/client:
import React from "react"; import { ApolloClient, InMemoryCache, createHttpLink, ApolloProvider } from "@apollo/client"; import PokemonsContainer from "./container/PokemonsContainer"; function App() { // Create an HTTP link to your GraphQL API const httpLink = createHttpLink({ uri: "https://graphql-pokemon2.vercel.app/", }); // Initialize the Apollo Client with cache and link const client = new ApolloClient({ link: httpLink, cache: new InMemoryCache(), }); return ( <ApolloProvider client={client}> <main> <PokemonsContainer /> </main> </ApolloProvider> ); } export default App;
5. Restart your project
Run the start command again, and the error should be gone:
npm start
Why this works
By removing conflicting old packages, we let npm properly resolve the dependency tree for @apollo/client v3, ensuring all required sub-dependencies (like optimism) are installed in the correct path.
内容的提问来源于stack exchange,提问作者BIJITH P N

