Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshotting ES Modules #3981

Open
krk opened this issue Sep 15, 2022 · 10 comments
Open

Snapshotting ES Modules #3981

krk opened this issue Sep 15, 2022 · 10 comments

Comments

@krk
Copy link

krk commented Sep 15, 2022

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
Copy link
Member

ovflowd commented Sep 15, 2022

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

@ovflowd ovflowd transferred this issue from nodejs/single-executable Sep 15, 2022
@legendecas
Copy link
Member

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
Copy link
Author

krk commented Sep 19, 2022

Can that script use top-level await somehow?

@joyeecheung
Copy link
Member

joyeecheung commented Sep 20, 2022

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
Copy link
Author

krk commented Feb 10, 2023

This may be relevant: denoland/deno#17460

@wenerme
Copy link

wenerme commented Jun 1, 2023

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

🕐 bootstrap need 40s 😭

SpringBoot era of nodejs


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

@wenerme
Copy link

wenerme commented Jun 1, 2023

xref nodejs/node#35711

Copy link

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 github-actions bot added the stale label Apr 27, 2024
@krk
Copy link
Author

krk commented Apr 30, 2024

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

@github-actions github-actions bot removed the stale label May 1, 2024
@mcollina
Copy link
Member

mcollina commented May 7, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants