Apollo-server: Correct way to import apollo-server (rather than require)

Created on 15 Jul 2018  路  15Comments  路  Source: apollographql/apollo-server

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

Most helpful comment

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).

All 15 comments

@meteorpublish Super odd, https://github.com/apollographql/apollo-server/blob/95f4d5e00b0d567a34463d7b27a9fdc3dfdf6eea/packages/apollo-server/src/index.test.ts#L7 uses the first style of import. Does Node 10 require that the package.json place the entry point somewhere other than main?

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

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

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!

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.

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

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

[email protected]
[email protected]
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.

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

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).

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?

@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.

Still getting an error when using Node v14.4.0

https://github.com/apollographql/apollo-server/issues/3786#issuecomment-652694891

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

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

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

Was this page helpful?
0 / 5 - 0 ratings