You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The "Quick start" page of the docs is confusing when starting a project from scratch. I was trying to experiment quickly with Yoga so I started a new project, installed graphql-yoga and graphql and created schema.js and index.js with the code from the snippets on the page.
Running this did not work as expected
$ node index.js
(node:5106) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/..../yoga-graphql/index.js:1
import { createServer } from 'node:http'
^^^^^^
SyntaxError: Cannot use import statement outside a module
Sure, I can add type: "module" to my package.json, but then I get an error about it not finding schema:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/.../yoga-graphql/schema' imported from /..../yoga-graphql/index.js
Did you mean to import "./schema.js"?
Patching the schema import line to import { schema } from './schema.js' works, and the server is able to start. 🎉
A Carefully crafted "Getting started" page makes a good first impression. While these issues can be common, I think the page could be updated to mention these tricks and make the first contact more pleasant.
I see two ways to fix this:
switch the code snippets to the (old and terrible) require() syntax.
Mention that code snippets use the module standard, suggest to set "type":"module" in the package.json file, and fix the schema.js path.
I'll happily submit a PR going whichever way you prefer, thanks!
The text was updated successfully, but these errors were encountered:
TPXP
changed the title
[docs] Suggest code snippets that work
docs(Getting started): Suggest code snippets that work without changes
May 20, 2024
Hi @TPXP, Thank you for reaching out about this!
We are in the course of improving our docs, and any feedback or help is very appreciated!
If you're willing to make a PR for this, I'm more in favor of making using the ESM standard. It's supposed to be the way to go for new project, so let's push this instead of pushing back to CJS syntax.
I think we are too TS minded in our docs, we should be more careful when we write JS snippets, because imports are slightly different than we are used to.
The "Quick start" page of the docs is confusing when starting a project from scratch. I was trying to experiment quickly with Yoga so I started a new project, installed
graphql-yoga
andgraphql
and createdschema.js
andindex.js
with the code from the snippets on the page.Running this did not work as expected
Sure, I can add
type: "module"
to mypackage.json
, but then I get an error about it not findingschema
:Patching the schema import line to
import { schema } from './schema.js'
works, and the server is able to start. 🎉A Carefully crafted "Getting started" page makes a good first impression. While these issues can be common, I think the page could be updated to mention these tricks and make the first contact more pleasant.
I see two ways to fix this:
require()
syntax."type":"module"
in thepackage.json
file, and fix theschema.js
path.I'll happily submit a PR going whichever way you prefer, thanks!
The text was updated successfully, but these errors were encountered: