Search code examples
typescriptapolloapollo-servergraphql-js

Error: Cannot find module/ApolloServer/protobufjs/minimal when starting ApolloServer for Express in production


I am creating an ApolloExpressServer using TypeScript. The code runs fine when running in development mode using

ts-node -r tsconfig-paths/register ./src --env=development

But after I build the project with tsc --build tsconfig.prod.json it builds successfully but fails when I try to start the server with node -r module-alias/register ./dist

It outputs an error

Error: Cannot find module '/dist/ApolloServer/protobufjs/minimal'
Require stack:
- /node_modules/apollo-engine-reporting-protobuf/dist/protobuf.js
- /node_modules/apollo-engine-reporting-protobuf/dist/index.js
- /node_modules/apollo-engine-reporting/dist/agent.js
- /node_modules/apollo-server-core/dist/ApolloServer.js
- /node_modules/apollo-server-core/dist/index.js
- /node_modules/apollo-server-express/dist/index.js

I am currently using

  • "apollo-server-express": "^2.17.0"
  • "typescript": "^3.9.3"

A link to the reproduction repo https://github.com/Oluwatemilorun/typescript-server


Solution

  • I found the issue. it was in happening because of the moduleAlias in my package.json. I had created a module aliases like

    {
        "name": "apollo-express-server",
        "version": "0.0.0",
            "_moduleAliases": {
            "@middlewares": "dist/middlewares",
            "@validators": "dist/validators",
            "@models": "dist/models",
            "@shared": "dist/shared",
            "@config": "dist/config",
            "@server": "dist/Server",
            "@apollo": "dist/ApolloServer"
        },
    }
    

    Removing the line "@apollo": "dist/ApolloServer" solved the issue. Apparently it was overriding the /node_modules/@apollo/* and pointing it into dist/ApolloServer