Last modified: Jul 02, 2026
Fix Apollo-Server Module Error
Getting the error "Cannot find module 'apollo-server'" stops your Node.js project. This is a common issue when building GraphQL APIs. The error means Node.js cannot locate the package. Do not worry. This guide shows you how to fix it quickly.
We will cover installation, version mismatches, and environment problems. Each step includes code examples. You will solve the error in minutes.
Why Does This Error Happen?
The error occurs because the apollo-server package is missing or not installed correctly. Node.js uses the require() function to load modules. If the module is not in the node_modules folder, you get this error.
Common causes include:
- You forgot to install the package.
- You installed it in the wrong project.
- You have a typo in the package name.
- Your
package.jsonis corrupted. - You are using an incompatible Node.js version.
Step 1: Install Apollo-Server Correctly
The first fix is to install the package. Run this command in your project root:
npm install apollo-server
This adds apollo-server to your node_modules folder. It also updates package.json and package-lock.json.
If you use yarn, run:
yarn add apollo-server
After installation, check your package.json. You should see "apollo-server" in the dependencies section.
Step 2: Verify the Module Path
Sometimes the module is installed but Node.js cannot find it. Check your file structure. Make sure your script is in the same project where you ran the install command.
Open your terminal and run:
ls node_modules | grep apollo-server
If you see apollo-server in the output, the module exists. If not, reinstall it.
Also verify your import statement. Use the correct syntax:
// Correct import for Apollo Server
const { ApolloServer } = require('apollo-server');
// Or if using ES modules
import { ApolloServer } from 'apollo-server';
Step 3: Clear npm Cache and Reinstall
A corrupted cache can cause module errors. Clear the cache and reinstall dependencies:
npm cache clean --force
rm -rf node_modules package-lock.json
npm install
This removes all installed packages and the lock file. A fresh install often fixes the issue.
Step 4: Check Node.js Version
Apollo Server requires Node.js version 12 or higher. Check your version:
node --version
If your version is below 12, update Node.js. Download the latest LTS version from the official website or use a version manager like nvm.
To update with nvm, run:
nvm install --lts
nvm use --lts
Step 5: Use the Correct Apollo Package
Apollo Server has several packages. For a basic setup, use apollo-server. For Express integration, use apollo-server-express. If you installed the wrong one, you get the error.
To install the Express version:
npm install apollo-server-express
Then update your import:
const { ApolloServer } = require('apollo-server-express');
Step 6: Example Code and Output
Here is a complete example to test your setup. Create a file called server.js:
// server.js - Simple Apollo Server
const { ApolloServer, gql } = require('apollo-server');
// Define schema
const typeDefs = gql`
type Query {
hello: String
}
`;
// Define resolver
const resolvers = {
Query: {
hello: () => 'Hello, world!',
},
};
// Create server
const server = new ApolloServer({ typeDefs, resolvers });
// Start server
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});
Run the server:
node server.js
Expected output:
Server ready at http://localhost:4000/
If you see this output, the module error is fixed. Open the URL in your browser to test the GraphQL playground.
Step 7: Check for Typos in package.json
Sometimes package.json has a typo. Open the file and check the dependencies section. It should look like this:
{
"name": "my-project",
"version": "1.0.0",
"dependencies": {
"apollo-server": "^3.0.0"
}
}
If the name is misspelled, correct it. Then run npm install again.
Step 8: Use a Global Installation (Not Recommended)
Avoid installing apollo-server globally. Global modules are not accessible by require() in your project. Always install locally.
If you already installed it globally, remove it:
npm uninstall -g apollo-server
Then install it locally as shown in Step 1.
Step 9: Handle Permissions Issues
On Linux or macOS, permission errors can prevent installation. Use sudo only as a last resort. Instead, fix your npm permissions:
npm config set prefix ~/.npm
Then reinstall the package.
Step 10: Check for Conflicting Modules
If you have multiple versions of apollo-server in your project, it causes conflicts. Check your node_modules folder for duplicates. Remove all and reinstall.
You can also use npm dedupe to clean up duplicates:
npm dedupe
Related Error: Fix Node.js Error: Cannot find module
If you encounter similar errors with other packages, refer to our guide on Fix Node.js Error: Cannot find module. It covers general troubleshooting for missing module errors in Node.js.
Conclusion
The "Cannot find module 'apollo-server'" error is easy to fix. Start by installing the package locally. Then verify your import path, clear the cache, and check your Node.js version. Use the example code to test your setup.
Remember to always install Apollo Server in your project directory. Avoid global installations. If the error persists, reinstall from scratch. With these steps, your GraphQL server will run smoothly.