Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: You must `await server.start()` before calling `server.applyMiddleware()`

After updating the apollo-server to version 3 the following error is shown in the console

C:\projects\my_project\node_modules\apollo-server-core\src\ApolloServer.ts:554
      throw new Error(
            ^
Error: You must `await server.start()` before calling `server.applyMiddleware()`
    at ApolloServer.assertStarted (C:\projects\my_project\node_modules\apollo-server-core\src\ApolloServer.ts:554:13)
    at ApolloServer.applyMiddleware (C:\projects\my_project\node_modules\apollo-server-express\src\ApolloServer.ts:60:10)
    at Object.<anonymous> (C:\projects\my_project\src\index.ts:17:14)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Module.m._compile (C:\projects\my_project\node_modules\ts-node\src\index.ts:1225:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Object.require.extensions.<computed> [as .ts] (C:\projects\my_project\node_modules\ts-node\src\index.ts:1228:12)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
[nodemon] app crashed - waiting for file changes before starting...

Adding server.start() before server.applyMiddleware() doesn't fix the problem. Any suggestions?

like image 275
Roman Mahotskyi Avatar asked Jun 05 '26 02:06

Roman Mahotskyi


1 Answers

This is what worked for me as alternative to await server.start()

server.start().then(res => {
  server.applyMiddleware({ app, path: '/' });

  app.listen({ port }, () => 
    console.log(`Gateway API running at port: ${port}`)
  );  
});

This post led me to the answer.

like image 90
chiusday Avatar answered Jun 08 '26 00:06

chiusday