Is the .clear
meta-command exactly the same as the .break
meta-command, or is it a variation of it? I know that they can both be used to break out of a ...
prompt (an indication that you haven't yet finished your command) without exiting the REPL, as can be done by hitting CTRL+C
. But are there any fundamental differences between .clear
and .break
?
I have read in numerous places that in the node.js REPL, the .clear
meta-command is meant to wipe out any variables or closures that you might have in memory without the need to restart the REPL.
Is the expected behavior such that I can declare a variable, run the .clear
command, and call that variable again, finding it empty/non-declared? In my use of the .clear
command, I haven't found that to work.
The .break
and .clear
commands behave differently depending on if you started the REPL from the node
command, or used repl.start()
.
When using the node
command, .clear
is just an alias for .break
. But if you start the REPL from repl.start()
, .clear
will then clear the local context as you are expecting, and .break
behaves as stated.
This is what .help
looks like from a REPL instance started from node
:
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.exit Exit the repl
.help Show repl options
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
And this is what it looks like when started programmatically:
.break Sometimes you get stuck, this gets you out
.clear Break, and also clear the local context
.exit Exit the repl
.help Show repl options
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
Using .clear
in a REPL started with repl.start()
will also show this:
Clearing context...
Here's an example of using the REPL programmatically:
var repl = require('repl');
var str = 'We can pass variables into a context.';
repl.start().context.str2 = str;
After this, we have a CLI open. If we type str2
then you will get the contents of str
. If you clear the context, then str2
will no longer exist inside the context.
Thanks @hexacyanide for your answer!
And these are two screenshots.
In the first screenshot, REPL was instantiated programmatically (by running the repltest.js
script), so the .clear
meta-command clears the variable from the memory. You can also see that the .help
command returns a different behavior for .clear
when REPL is instantiated by a script.
In the second screenshot, .clear
has a different behavior however. Here REPL is instantiated directly via command (simply by running node in CMD
). In this case, the variables are not cleared from the memory, and the .help
command only tells us that .clear
is an alias for .break
.
Thanks again hexacyanide for clarifying this.