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

REPL: make -r work in ESM mode #44094

Closed
wmertens opened this issue Aug 2, 2022 · 9 comments
Closed

REPL: make -r work in ESM mode #44094

wmertens opened this issue Aug 2, 2022 · 9 comments
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@wmertens
Copy link

wmertens commented Aug 2, 2022

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, and our-repl.js adds useful globals.

However, in ESM mode this doesn't work. -r is literally translated to require().

What is the feature you are proposing to solve the problem?

It would be great if -r foo in ESM mode translates to await 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.

@wmertens wmertens added the feature request Issues that request new features to be added to Node.js. label Aug 2, 2022
@targos
Copy link
Member

targos commented Aug 2, 2022

I think #43942 is exactly what you need.

@MoLow
Copy link
Member

MoLow commented Aug 2, 2022

I think #43942 is exactly what you need.

FYI I don't think it was released yet

@aduh95
Copy link
Contributor

aduh95 commented Aug 2, 2022

There is no real alternative

Untrue, you can use --loader while you wait for --import to be released, but just know that's a short term solution which will break soon™.

@wmertens
Copy link
Author

wmertens commented Aug 2, 2022

Untrue, you can use --loader while you wait for --import to be released,

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?

but just know that's a short term solution which will break soon™

? Is --loader going away?

@wmertens
Copy link
Author

wmertens commented Aug 2, 2022

Thanks @targos, that is indeed great. I didn't realize -r was not part of the REPL :-)

Closing as a duplicate of #40110

@wmertens wmertens closed this as completed Aug 2, 2022
@aduh95
Copy link
Contributor

aduh95 commented Aug 2, 2022

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?

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

Is --loader going away?

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, --import would be available and a better fit for your use-case.

@wmertens
Copy link
Author

wmertens commented Aug 2, 2022

Thanks @aduh95, that's a nice workaround for now!

@wmertens
Copy link
Author

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.

@dgroomes
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

5 participants