Graphql-tools: Error importing with --experimental-modules

Created on 1 Aug 2018  路  8Comments  路  Source: ardatan/graphql-tools

I've pasted this example in an .mjs file. When I attempt to run it with node v10.5.0 with the --experimental-modules flag, I get this error:

import { makeExecutableSchema } from 'graphql-tools';
         ^^^^^^^^^^^^^^^^^^^^
SyntaxError: The requested module 'graphql-tools' does not provide an export named 'makeExecutableSchema'
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:80:21)
enhancement

Most helpful comment

@dandv --experimental-modules doesn't support importing named exports from a commonjs module (except node's own built-ins). if you want to use mjs files, which I don't recommend, you would need to import graphql-tools either with default import or by importing the namespace

import graphqlTools from 'graphql-tools'
// or
import * as graphqlTools from 'graphql-tools'

// and then get your `makeExecutableSchema` from there:
const { makeExecutableSchema } = graphqlTools

_edit:_

you might like: https://github.com/standard-things/esm. you can keep the js file extension and get named imports from cjs modules.

All 8 comments

Do you know what is required for an npm package to be compatible with this experimental modules implementation? Perhaps there's some TypeScript feature we can turn on?

@dandv --experimental-modules doesn't support importing named exports from a commonjs module (except node's own built-ins). if you want to use mjs files, which I don't recommend, you would need to import graphql-tools either with default import or by importing the namespace

import graphqlTools from 'graphql-tools'
// or
import * as graphqlTools from 'graphql-tools'

// and then get your `makeExecutableSchema` from there:
const { makeExecutableSchema } = graphqlTools

_edit:_

you might like: https://github.com/standard-things/esm. you can keep the js file extension and get named imports from cjs modules.

@stubailo: yes, the modules property in tsconfig.json needs to be set to "es2015" (now it's "commonjs"). Then .js files need to be renamed to .mjs.

Here's a gist outputting both.

I just left a comment on that Gist, explaining what needs to happen for native ESM support.

@jaydenseric Unfortunately GitHub doesn't send notifications for comments on gists.

https://github.com/isaacs/github/issues/21#issuecomment-239340234

Please see https://github.com/facebook/jest/issues/4637#issuecomment-425064109.

graphql-tools [have] transpiled away their import/export, so it's not compatible with babel and actual ESModules at the same time

Another option is to continue outputting CommonJS until Node 12 drops the --experimental-flag, but export everything as default (suggestion from Babel contributor @nicolo-ribaudo):

They could this code to their entry point:

import * as mod from "./this-file.js";
export default mod;

By doing so, everything is exported both as a named export and as a property of the default export.

Should be fixed by #1329, rolling into #1306

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

proehlen picture proehlen  路  4Comments

flippidippi picture flippidippi  路  3Comments

stubailo picture stubailo  路  3Comments

adamkl picture adamkl  路  3Comments