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

Correct way to import apollo-server (rather than require) #1356

Closed
tredondo opened this issue Jul 15, 2018 · 18 comments
Closed

Correct way to import apollo-server (rather than require) #1356

tredondo opened this issue Jul 15, 2018 · 18 comments

Comments

@tredondo
Copy link

I'm trying to import 'apollo-server' using --experimental-modules in Node 10. What is the correct syntax?

import { ApolloServer } from 'apollo-server';
// SyntaxError: The requested module 'apollo-server' does not provide an export named 'ApolloServer'
import ApolloServer from 'apollo-server'
// IDE warning: "Default export is not declared in imported module
@ghost ghost added the 📝 documentation Focuses on changes to the documentation (docs) label Jul 15, 2018
@evans
Copy link
Contributor

evans commented Jul 16, 2018

@meteorpublish Super odd,

import { gql, ApolloServer } from './index';
uses the first style of import. Does Node 10 require that the package.json place the entry point somewhere other than main?

@TimPerry
Copy link

TimPerry commented Aug 1, 2018

+1 on this, also trying to use the flag with node 10.

@justinanastos justinanastos removed the 📝 documentation Focuses on changes to the documentation (docs) label Apr 16, 2019
@andreisocaciu
Copy link

Any news about support on node 12 with --experimental-modules flag on?

@trevorblades trevorblades added the 🚧👷‍♀️👷‍♂️🚧 in triage Issue currently being triaged label Jul 8, 2019
@trevorblades
Copy link
Member

The correct syntax is this one:

import {ApolloServer} from 'apollo-server'; // or apollo-server-[your framework]

I was able to get this to work using https://github.com/standard-things/esm. Closing this issue now, please reopen if you have more questions!

@abernix abernix removed 🚧👷‍♀️👷‍♂️🚧 in triage Issue currently being triaged labels Jul 9, 2019
@rolfen
Copy link

rolfen commented Aug 8, 2019

The correct syntax is this one:

import {ApolloServer} from 'apollo-server'; // or apollo-server-[your framework]

I was able to get this to work using https://github.com/standard-things/esm. Closing this issue now, please reopen if you have more questions!

I was not - it complains that

SyntaxError: The requested module 'file:///home/me/code/test/node_modules/apollo-server/dist/index.js' does not provide an export named 'ApolloServer'

Just saying.

@trevorblades
Copy link
Member

@rolfen what version of apollo-server are you using?

@rolfen
Copy link

rolfen commented Aug 8, 2019

@rolfen what version of apollo-server are you using?

apollo-server@2.8.1
esm@3.2.25
node v12.7.0

This is how I am calling the code:
node -r esm ./server.mjs

After some tests, I realized that just changing the file extension from .mjs to .js clears the error.

@trevorblades
Copy link
Member

trevorblades commented Aug 8, 2019

That's interesting that changing the extension would fix it. Normally, esm allows you to use import/export syntax in .js files. Here's a codesandbox I put together showing an example ESM + Apollo Server setup: https://codesandbox.io/s/polished-http-09vyl

@djfarrelly
Copy link

Using "type": "module" in package.json with Node 12 and --experimental-modules I had to use this workaround:

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

Instead of what I assumed would work:

import { ApolloServer } from 'apollo-server'

Which throws the The requested module 'apollo-server' does not provide an export named 'ApolloServer' as others mentioned above. I wanted to share this workaround in case anyone else is running into the same issues using the --experimental-modules flag or using modules w/ node 13 w/out the flag (docs).

@dandv
Copy link
Contributor

dandv commented Dec 13, 2019

Would be nice to have proper ES Modules support (without esm), now that they're no longer experimental since Node v13.2.0.

@trevorblades should this issue be reopened?

@trevorblades
Copy link
Member

@dandv @djfarrelly it looks like we could support ES modules and CommonJS at the same time by following a pattern like the ones mentioned in the NodeJS API reference.

That being said, Apollo Server is written in TypeScript and compiled to be compatible with CommonJS. We're passing the esModuleInterop flag to the TS compiler, so it's surprising that named exports aren't working as expected using native NodeJS ES modules. I found a few issues on the TypeScript repo that might relate to this:

If there was a way to replicate the pattern identified in the NodeJS reference above using TypeScript config options, that would be ideal.

@platocrat
Copy link

Still getting an error when using Node v14.4.0

#3786 (comment)

@boblitex
Copy link

The requested module 'apollo-server-express' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.

I tried to import ApolloServer using Es6 syntax and the above was the error output. something isn't right with that still.
using Node 14.8.0 and module support is enabled

@dandv
Copy link
Contributor

dandv commented Sep 7, 2020

I've managed to solve this exact problem (named ESM imports from the TypeScript output) in a package I'm maintaining, including the error message reported by @boblitex, by using conditional exports. These launched in Node 13.7.0, and have been no longer experimental since v13.10.

You can see a diff implementing this for that package I'm maintaining.

What needs to be done, essentially, is to output .mjs files (might require manual renames for now) and add the following to the various package.json files:

"exports": {
  "node": {
    "import": "./index.mjs",
    "require": "./index.js"
  },
"default": "./index.mjs"
},

@trevorblades

@jaibatrik
Copy link

Shouldn't we keep this issue open for tracking since the problem still persists?

@mushketyk
Copy link

@trevorblades This issue still persists with the 3.0.2 version of apollo-server. Should it be reopened?

@trevorblades
Copy link
Member

@mushketyk I'm doing import {ApolloServer} from 'apollo-server' with Apollo Server 3, Node 14, and "type": "module" in my package.json. Works perfectly fine as far as I can tell. Can you describe the issue you're seeing?

@glasser
Copy link
Member

glasser commented Aug 3, 2021

Let's keep this issue closed because it's a duplicate of #3786. @trevorblades cool to hear it's working for you, though that's surprising. Should we close #3786?

@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