arackaf / mongo-graphql-starter

Creates a fully functioning, performant, extensible GraphQL endpoint from a Mongo DB. Supports middleware, Mongo 4 transactions.
MIT License
423 stars 29 forks source link

express-graphql does not provide an export named 'default' #79

Closed peebles closed 3 years ago

peebles commented 3 years ago

Using your simple example of a server, which includes a line that looks like

import expressGraphql from "express-graphql";

Throws an error. You can see this using the repl:

$ node -r esm
Welcome to Node.js v12.12.0.
Type ".help" for more information.
> import expressGraphql from "express-graphql";
Thrown:
/Users/peebles/zhortech/graphql/node_modules/esm/esm.js:1
SyntaxError: The requested module 'file:///Users/peebles/zhortech/zql/node_modules/express-graphql/index.js' does not provide an export named 'default'
> 
   "express-graphql": "^0.12.0",
arackaf commented 3 years ago

Hm thanks - I’ll have a look

peebles commented 3 years ago

My "fix" for this in in main.js:

const { graphqlHTTP } = require('express-graphql');

and

app.use( "/graphql", graphqlHTTP({ schema: executableSchema, graphiql: true, rootValue: root }) );

arackaf commented 3 years ago

Cool - I'll have a look at this soon. Thanks for raising this.

arackaf commented 3 years ago

Hey so this one is interesting. It looks like you're using Node's native esm, which is causing the failure. My code assumes you're using the esm package off of npm (which will work). What's interesting is that none of my generated code here will work with Node's native esm - it all assumes you're using the esm package.

If you can't / don't want to use the esm package off of npm, then using require for express-graphql here would absolutely be the correct fix, but like I said you'll have a lot of trouble getting all the generated graphql code to work.

I'll look at making this clearer in the docs.

peebles commented 3 years ago

I am a "require" kind of old-timer. I am using node 12.12. I believe I am using the esm package:

$ ls node_modules/esm/
LICENSE     README.md   esm     esm.js      index.js    node_modules    package.json

and

$ cat index.js
require = require("esm")(module/* , options */)
module.exports = require("./main.js")

and:

$ node main.js
/Users/peebles/zhortech/zql/main.js:1
import { MongoClient } from "mongodb";
^^^^^^

SyntaxError: Cannot use import statement outside a module
arackaf commented 3 years ago

@peebles in your example above, I believe you want

$ node index.js