import GraphQLSomeObject from 'graphql' throws an error in node
Hi,
a simple import statement in node like
import { GraphQLObjectType } from 'graphql';
results in a syntax error:
SyntaxError: The requested module 'graphql' does not provide an export named 'GraphQLObjectType'
going to the right index.mjs file directly works:
import { GraphQLObjectType } from 'graphql/index.mjs';
This is in node 13.14, without using the esm module to run the code. It is probably because conditional exports for commonJS/ES6 modules are needed.
@lenneis Can you please create a minimal repository that reproduces that?
Done, at https://github.com/lenneis/graphql-es6-bug.git. I have included the graphql module in the repository. Run with "node index.mjs".
I just tested with node 14.5 as well. The error message changes to this:
import { GraphQLObjectType } from 'graphql';
^^^^^^^^^^^^^^^^^
SyntaxError: The requested module 'graphql' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
Why would an import statement in an .mjs file expect a CommonJS module?
anyone have a solution for this?
I haven't run across this issue, but I believe it's just a node limitation and not anything specific to GraphQL.
As the error message suggests, try importing the CommonJS module object from its default export instead of using named imports:
import graphql from "graphql";
const { GraphQLObjectType } = graphql;