nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.46k stars 279 forks source link

Snapshotting ES Modules #3981

Open krk opened 1 year ago

krk commented 1 year ago

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.

ovflowd commented 1 year ago

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

legendecas commented 1 year ago

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

krk commented 1 year ago

Can that script use top-level await somehow?

joyeecheung commented 1 year ago

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)

krk commented 1 year ago

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

wenerme commented 1 year ago

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

🕐 bootstrap need 40s 😭

SpringBoot era of nodejs


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

wenerme commented 1 year ago

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

github-actions[bot] commented 4 months ago

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.

krk commented 4 months ago

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
mcollina commented 4 months ago

@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