Apollo-server: Support ES modules

Created on 12 Feb 2020  路  3Comments  路  Source: apollographql/apollo-server

import { ApolloServer } from 'apollo-server'

does not work in Node 13 with "type": "module" in package.json. More info here: https://github.com/apollographql/apollo-server/issues/1356#issuecomment-567101161

import { ApolloServer } from 'apollo-server'
         ^^^^^^^^^^^^
SyntaxError: The requested module 'apollo-server' does not provide an export named 'ApolloServer'
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:92:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:107:20)
    at async Loader.import (internal/modules/esm/loader.js:164:24)

Most helpful comment

The workaround that I found (from https://github.com/apollographql/apollo-server/issues/1356) is

import apollo from 'apollo-server'
const { ApolloServer } = apollo

All 3 comments

The workaround that I found (from https://github.com/apollographql/apollo-server/issues/1356) is

import apollo from 'apollo-server'
const { ApolloServer } = apollo

The workaround that I found (from #1356) is

import apollo from 'apollo-server'
const { ApolloServer } = apollo

Using that suggested solution with Node v14.4.0 fails with the following implementation:

const express = require('express')
import apollo from 'apollo-server'
const { ApolloServer ) = apollo

const typeDefs = require('./schema.js')
const resolvers = require('./resolvers')
const models = require('./models')

const server = new ApolloServer({
    typeDefs,
    resolvers,
    // Pass models as context to Apollo Server to give us access
    // to models from our resolvers.
    context: { models }
})

const app = express()
server.applyMiddleware({ app })

app.listen({ port: 4000 }, () =>
       console.log(`馃殌Server ready at http://localhost:4000${server.graphqlPath}`)
      )

The following is the error:

(node:36894) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/platocrat/Documents/neuo/server/src/index.js:2
import apollo from 'apollo-server'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1116:16)
    at Module._compile (internal/modules/cjs/loader.js:1164:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

And after correcting for that in the package.json, I get the following error on requiring express in my index.js file for Apollo Server:

platocrat@platocrats-MacBook-Pro server % node src/index.js
file:///Users/platocrat/Documents/neuo/server/src/index.js:1
const express = require('express')
                ^

ReferenceError: require is not defined
    at file:///Users/platocrat/Documents/neuo/server/src/index.js:1:17
    at ModuleJob.run (internal/modules/esm/module_job.js:138:23)
    at async Loader.import (internal/modules/esm/loader.js:178:24)

I'm going to declare this out of scope for AS3 for now. Hopefully when this is needed we can do it in a way that's backwards-compatible and doesn't require a major version bump.

Was this page helpful?
0 / 5 - 0 ratings