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

ECMAScript module loading error #40

Closed
ghost opened this issue Jan 29, 2018 · 8 comments
Closed

ECMAScript module loading error #40

ghost opened this issue Jan 29, 2018 · 8 comments
Labels

Comments

@ghost
Copy link

ghost commented Jan 29, 2018

Thank you for your great package. I have one question:

When using the program with ESM (.mjs import) I get an error with the current version of Node (9.4.0).

(node:4290) ExperimentalWarning: The ESM module loader is experimental.
file:///Users/.../node_modules/apollo-upload-server/lib/types.mjs:1
import { GraphQLScalarType } from 'graphql'
         ^^^^^^^^^^^^^^^^^
SyntaxError: The requested module does not provide an export named 'GraphQLScalarType'
    at ModuleJob._instantiate (internal/loader/ModuleJob.js:86:19)
    at <anonymous>
error Command failed with exit code 1.

The problem should be resolved by changing the following lines in types.mjs:

import graphql from 'graphql'

const { GraphQLScalarType } = graphql

I think GraphQL exports currently only with CJS and not yet with ESM and therefore there is only the "default export". Since you are running this program with Babel, you will not get an error. I use the package directly with Node.

https://nodejs.org/api/esm.html#esm_interop_with_existing_modules

@jaydenseric
Copy link
Owner

I was using vanilla --experimental-modules, but have recently been using @std/esm in cjs mode so it's possible this slipped under the radar. The intent is to be 100% vanilla .mjs compatible. Looking into it now…

@ghost
Copy link
Author

ghost commented Jan 29, 2018

I started the program also with node --experimental-modules ./server.mjs and @std/esm, but no success. The same without @std/esm. Only the error message is more informative. I found no solution other than import by default and destructuring the imported default object. Thank you for your help.

@jaydenseric
Copy link
Owner

Hang on, the fix is not working 😱

@jaydenseric jaydenseric reopened this Jan 29, 2018
@jaydenseric
Copy link
Owner

jaydenseric commented Jan 29, 2018

The issue is that this in the source works correctly for .mjs, but breaks in the .js:

import graphql from 'graphql'
export const GraphQLUpload = new graphql.GraphQLScalarType({})

While this breaks the .mjs but works for the .js:

import * as graphql from 'graphql'
export const GraphQLUpload = new graphql.GraphQLScalarType({})

Not sure if it is a Babel bug or what.

@ghost
Copy link
Author

ghost commented Jan 29, 2018

Ok, maybe you can use a temporary solution with a mix of default and variable import like this one:
import myDefault, * as myModule from "my-module";
Not sure if it works for both, vanilla Node and Babel.

And then you can add a conditional assignment like that:
const graphql = myDefault || myModule

@jaydenseric
Copy link
Owner

Related? babel/babel#6983

@jaydenseric
Copy link
Owner

This should be fixed in v4.0.2. I'll continue to investigate a better solution.

I've updated the example API to use native modules instead of @std/esm.

@ghost
Copy link
Author

ghost commented Jan 30, 2018

Now, it works - thanks 👍

jaydenseric referenced this issue in jaydenseric/extract-files Feb 17, 2018
krasivyy3954 added a commit to krasivyy3954/react-graphql-upload that referenced this issue Jan 6, 2023
krasivyy3954 added a commit to krasivyy3954/react-graphql-upload that referenced this issue Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant