responseCachePlugin() is not a function

Hi, I am trying to learn GraphQL and how to use the server side caching but when I import responseCachePlugin and add it to my server I get an error that responseCachePlugin() is not a function. Does anyone know why this is happening? Thank you in advance.


import {ApolloServer, gql} from "apollo-server-express";
import express from 'express';
import http from 'http';
import './models/db.js';
import {typeDefs} from './typeDefs.js';
import {resolvers} from './resolvers.js';
import responseCachePlugin from 'apollo-server-plugin-response-cache';
 
async function startApolloServer(typeDefs, resolvers){
    const app = express();
    const httpServer = http.createServer(app);
    const server = new ApolloServer({
        typeDefs,
        resolvers,
        cacheControl: true,
        corsOptions: {origin: '*'},
        plugins: [responseCachePlugin()],
    
    });

    await server.start();
    server.applyMiddleware({
        app,
        path: '/graphql'
    })
    await new Promise(resolve => httpServer.listen({port:4000}, resolve));
    console.log(`🚀 Server ready at http://localhost:4000`);
}

startApolloServer(typeDefs, resolvers);

Without a better look into your project (I can’t see your package.json), my best guess is that you haven’t installed the apollo-server-plugin-response-cache package.

If that’s not the issue, can you provide us with a runnable reproduction (GH repo or codesandbox) so we can help investigate further?

Here is my project

Apollo Server and friends are built for CommonJS and this project is using ES Modules. If you console.log(responseCachePlugin) you’ll see it’s being imported as an object with a default property on it. You should be able to call responseCachePlugin.default() to get the desired effect if you aren’t doing any transpilation and wish to continue using ESM.

My understanding of CJS and ESM could certainly be better, so there might be a better answer to this problem. In any case, I’ll open a ticket for us to investigate ESM support for ASv4.

It seems that responseCachePlugin.default() works correctly. I will consider switching from ESM to CJS in the future as I want to learn how graphql and apollo work for now.
Thank you very much for your answer and explanation, it helped me a lot.

1 Like