Open krk opened 1 year ago
I'm not sure you posted this in the correct repository. Moving this to /help 🙂
It was intentionally limited to the single file script as userland module loaders (both CJS and ESM) are not available at the time of building the startup snapshot. This is documented behavior.
/cc @joyeecheung
Can that script use top-level await somehow?
Not for now. The script is not a module, while TLA is a module thing. We could implement support for snapshotting a single file module but that still requires snapshot support for the module loader (even just an internal one) which is a fair bit of work, though I plan to get it done some time this year. For now as the documentation suggests, users need to bundle their application first before building a snapshot (in the case of modules, transpilation needs to be done to convert it into a script with require() instead of import)
This may be relevant: https://github.com/denoland/deno/pull/17460
main.mjs 10mb main.mjs.map 20mb
🕐 bootstrap need 40s 😭
SpringBoot era of nodejs
without --enable-source-maps
bootstrap in 2s 😮
There has been no activity on this issue for 11 months. The help repository works best when sustained engagement moves conversation forward. The issue will be closed in 1 month. If you are still experiencing this issue on the latest supported versions of Node.js, please leave a comment.
Tested with Node 20.10.0, issue still exists:
echo 'await 5;' > index.mjs
node --build-snapshot index.mjs
/krk/esm-snap/index.mjs:1
await 5;
^^^^^
SyntaxError: await is only valid in async functions and the top level bodies of modules
at minimalRunCjs (node:internal/main/mksnapshot:169:16)
at main (node:internal/main/mksnapshot:183:5)
at node:internal/main/mksnapshot:187:1
@krk have you tried using the new NODE_COMPILE_CACHE
released with Node v22.1.0? It speeds startup time massively for me.
ES Modules cannot be snapshotted now, the configure time parameter
--node-snapshot-main
fails with the errorwhich in turn points to https://github.com/nodejs/node/blob/v18.9.0/src/node_snapshotable.cc#L1198-L1224.
I was wondering if this is expected and is supporting ES Module snapshots viable.