Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ES modules #3786

Closed
lorensr opened this issue Feb 12, 2020 · 7 comments
Closed

Support ES modules #3786

lorensr opened this issue Feb 12, 2020 · 7 comments

Comments

@lorensr
Copy link
Contributor

lorensr commented Feb 12, 2020

import { ApolloServer } from 'apollo-server'

does not work in Node 13 with "type": "module" in package.json. More info here: #1356 (comment)

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)
@abernix abernix added this to the Release 3.x milestone Feb 21, 2020
@derekjwilliams
Copy link

The workaround that I found (from #1356) is

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

derekjwilliams added a commit to derekjwilliams/some-gears that referenced this issue Mar 28, 2020
@platocrat
Copy link

platocrat commented Jul 1, 2020

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)

@glasser
Copy link
Member

glasser commented May 4, 2021

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.

@glasser glasser removed this from the Release 3.x milestone May 4, 2021
@trevorblades
Copy link
Contributor

trevorblades commented Aug 3, 2021

I've put together a small example showing that nodejs imports are working with AS3 in Node 14: https://codesandbox.io/s/suspicious-wave-9z3r8?file=/src/index.js

Note that I've added "type": "module" to my package.json to make this work.

@glasser
Copy link
Member

glasser commented Aug 3, 2021

Hmm. While we didn't actually do any particular work to make ESM work, I agree with @trevorblades that it does seem to work, at least with Node 14!

The comments from @lorensr and @derekjwilliams predate Node 14.

@platocrat 's comment first is about what happens when you lack "type": "module", and then is about a require in their own code, not in Apollo Server.

This comment references an error message that this comment suggests is resolved in Node v14.13 (and v12.20). (I think this PR is the relevant one.)

This comment from @jaibatrik and this comment from @mushketyk don't have enough information to reproduce.

I'm going to assume that this issue is now fixed, as long as you're on Node v14.13 or v12.20! If anyone (esp @jaibatrik or @mushketyk) can demonstrate ESM imports not working with one of those versions of Node (perhaps start by forking @trevorblades 's sandbox above) then we can reopen it.

(Separately, it might be nice to publish apollo-server built as ESM itself, but I don't know if that actually helps with any specific use cases. For Apollo Client we're doing that so you can load directly from a site like esm.run, but that seems less relevant for a server project.)

@glasser glasser closed this as completed Aug 3, 2021
@alfaproject
Copy link
Contributor

@glasser it's very relevant for a server project because whoever uses Serverless functions like we do will always bundle them and ESM gives the best tree-shaking due to improve module dependency analysis by bundlers.

I'm not sure if this issue should be reopened or another created but it would be a big plus for the community if Apollo Server was shipped with ESM files as well.

@glasser
Copy link
Member

glasser commented Aug 18, 2021

Sure, we can make a new issue, but it would be great if that issue came with a clear example of something that doesn't work today. (This issue was about import not working in ESM mode, which appears to be fixed!)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants