-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
REPL: make -r work in ESM mode #44094
Comments
I think #43942 is exactly what you need. |
FYI I don't think it was released yet |
Untrue, you can use |
Do you mean a loader that prepends the await import statement to the very first script it is called to load? Which will break regular import statements that should go first? And if nothing is loaded, it won't run?
? Is --loader going away? |
I mean that you can use the flag to run code that has nothing to do with loaders, e.g. to define globals using ESM syntax / top-level await: $ node --loader 'data:text/javascript,globalThis.test=await Promise.resolve(4)'
(node:51788) ExperimentalWarning: Custom ESM Loaders is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Welcome to Node.js v18.7.0.
Type ".help" for more information.
> test
4
Sorta, it's going off-thread – so if you use it to e.g. define a global variable, it will be defined on the loader thread global scope and won't be available in the REPL anymore – but once this land, |
Thanks @aduh95, that's a nice workaround for now! |
Additionally, it turns out that the repl is just available for importing as "node:repl" and so it's way nicer to make a custom repl with that, I now have a custom prompt and cleanly injected variables. |
This was the key insight for me. Thanks for sharing @wmertens. For onlookers: here is a working example of starting a Node.js REPL programmatically and customizing it with some global variables that are imports from other code: https://github.com/dgroomes/javascript-playground/blob/b82d722ba473a81b5513d29f163e0572759c2b2f/repl/launch-repl.mjs |
What is the problem this feature will solve?
We use the REPL feature internally for doing changes to databases etc with the library functions that we have in the app. It's very handy.
Generally we do something like
node -r our-repl.js
, andour-repl.js
adds useful globals.However, in ESM mode this doesn't work.
-r
is literally translated torequire()
.What is the feature you are proposing to solve the problem?
It would be great if
-r foo
in ESM mode translates toawait import("foo")
.This wouldn't break anything, since right now
-r
simply doesn't work in ESM mode.What alternatives have you considered?
There is no real alternative; Right now we're using a script that uses terminal commands to stuff the terminal input buffer with
await import('foo')
and then runs the REPL, but that only works on Linux.The text was updated successfully, but these errors were encountered: