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

Error importing with --experimental-modules #913

Closed
dandv opened this issue Aug 1, 2018 · 8 comments
Closed

Error importing with --experimental-modules #913

dandv opened this issue Aug 1, 2018 · 8 comments

Comments

@dandv
Copy link
Contributor

dandv commented Aug 1, 2018

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)
@ghost ghost added the blocking Prevents production or dev due to perf, bug, build error, etc.. label Aug 1, 2018
@stubailo
Copy link
Contributor

stubailo commented Aug 1, 2018

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?

@stubailo stubailo added enhancement and removed blocking Prevents production or dev due to perf, bug, build error, etc.. labels Aug 1, 2018
@dnalborczyk
Copy link
Contributor

dnalborczyk commented Aug 20, 2018

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

@dandv
Copy link
Contributor Author

dandv commented Sep 26, 2018

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

@jaydenseric
Copy link

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

@dandv
Copy link
Contributor Author

dandv commented Sep 26, 2018

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

isaacs/github#21 (comment)

@dandv
Copy link
Contributor Author

dandv commented Sep 28, 2018

Please see jestjs/jest#4637 (comment).

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

@dandv
Copy link
Contributor Author

dandv commented May 5, 2019

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.

@yaacovCR
Copy link
Collaborator

yaacovCR commented Apr 1, 2020

Should be fixed by #1329, rolling into #1306

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants