help icon indicating copy to clipboard operation
help copied to clipboard

Snapshotting ES Modules

Open krk opened this issue 3 years ago • 10 comments
trafficstars

ES Modules cannot be snapshotted now, the configure time parameter --node-snapshot-main fails with the error

SyntaxError: Cannot use import statement outside a module
    at main (node:internal/main/mksnapshot:129:33)
    at node:internal/main/mksnapshot:169:1

which 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.

krk avatar Sep 15 '22 18:09 krk

I'm not sure you posted this in the correct repository. Moving this to /help 🙂

ovflowd avatar Sep 15 '22 21:09 ovflowd

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

legendecas avatar Sep 19 '22 01:09 legendecas

Can that script use top-level await somehow?

krk avatar Sep 19 '22 10:09 krk

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)

joyeecheung avatar Sep 20 '22 01:09 joyeecheung

This may be relevant: https://github.com/denoland/deno/pull/17460

krk avatar Feb 10 '23 13:02 krk

main.mjs 10mb main.mjs.map 20mb

🕐 bootstrap need 40s 😭

SpringBoot era of nodejs


without --enable-source-maps bootstrap in 2s 😮

wenerme avatar Jun 01 '23 16:06 wenerme

xref https://github.com/nodejs/node/issues/35711

wenerme avatar Jun 01 '23 16:06 wenerme

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.

github-actions[bot] avatar Apr 27 '24 01:04 github-actions[bot]

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 avatar Apr 30 '24 15:04 krk

@krk have you tried using the new NODE_COMPILE_CACHE released with Node v22.1.0? It speeds startup time massively for me.

https://nodejs.org/en/blog/release/v22.1.0

mcollina avatar May 07 '24 17:05 mcollina