Trouble with ESM-style import in Node.js Quickstart
Hello, I'm new to convex and working through the Quickstart documentation. I've run through the steps detailed here - https://docs.convex.dev/quickstart/nodejs but unfortunately am after running through all the steps. My environment is VS Code, BASH terminal and Node.JS 20.6.
Upon running the final script.js I get this return.
Unfortunately this is very deep-end-of-the-pool for me, but just giving my feedback as my first experience to following the quick-start that I'm essentially stuck.
Node.js Quickstart | Convex Developer Hub
Add Convex to a Node.js project
6 Replies
It's worth noting that the steps prior to script.js have worked well without error.
You can either:
1. Add
"type": "module"
to your package.json
, or
2. Rename script.js
to script.mjs
Docs: https://nodejs.org/api/esm.html#enabling@Karlstens If you can use ESM (Also I think @erquhart's option 1 will work but option 2 may not, since this error is coming from codegen'd code rather than script.js turns out that neither of these works, because the codegen'd api file was ESM syntax being interpreted as CJS regardless
Also thanks for the feedback! You're right, the quickstart is broken!
@Karlstens I just updated the quickstart, thanks again for reporting. The first step is different and the code is different. https://docs.convex.dev/quickstart/nodejs
"type": "module"
) like @erquhart suggests I'd do that. A third option here is to use require()
syntax — if you're using anyApi
this will just work. If you want to use require()
syntax with a typed API object you'll need to follow these instructions https://docs.convex.dev/client/javascript/node#javascript-with-commonjs-require-syntax to change the way codegen works.
(this is a newly supported approach for folks who can't use ESM for some reason)
Outstanding response - I'll check it in a moment. Is this the best method for feedback as I work through these articles?
Here is great. If you'd rather bundle them up in an email I'm tom@convex.dev, I've recently made a number of changes to docs. But DIscord is good too
Thanks for updating the doco Tom, the steps work well and without error. 👍🏻 I'm now wondering how I can re-run step 5 (JSONL data) over-and-over to keep adding data into my Convex project - I'm sure I'll discover how to do this soon enough though.