Karlstens
Karlstens2y ago

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.
$ node script.js
C:\DEV\VS Code\Convex\my-project\convex\_generated\api.js:12
import { anyApi } from "convex/server";
^^^^^^

SyntaxError: Cannot use import statement outside a module
$ node script.js
C:\DEV\VS Code\Convex\my-project\convex\_generated\api.js:12
import { anyApi } from "convex/server";
^^^^^^

SyntaxError: Cannot use import statement outside a module
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
Karlstens
KarlstensOP2y ago
It's worth noting that the steps prior to script.js have worked well without error.
erquhart
erquhart2y ago
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
ballingt
ballingt2y ago
@Karlstens If you can use ESM ("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) 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
Karlstens
KarlstensOP2y ago
Outstanding response - I'll check it in a moment. Is this the best method for feedback as I work through these articles?
ballingt
ballingt2y ago
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
Karlstens
KarlstensOP2y ago
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.

Did you find this page helpful?