graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

import GraphQLSomeObject from 'graphql' throws an error in node

Open lenneis opened this issue 5 years ago • 5 comments

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 avatar Jul 01 '20 20:07 lenneis

@lenneis Can you please create a minimal repository that reproduces that?

IvanGoncharov avatar Jul 02 '20 19:07 IvanGoncharov

Done, at https://github.com/lenneis/graphql-es6-bug.git. I have included the graphql module in the repository. Run with "node index.mjs".

lenneis avatar Jul 03 '20 09:07 lenneis

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?

lenneis avatar Jul 03 '20 10:07 lenneis

anyone have a solution for this?

karansinghgit avatar Nov 15 '20 19:11 karansinghgit

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;

mike-marcacci avatar Nov 15 '20 20:11 mike-marcacci