Search code examples
javascriptnode.jsread-eval-print-loop

How do I clear all variables in node REPL?


I open a REPL on my terminal with node or node -i.

After playing around, I’d like to clear everything in this REPL (chiefly the JS variables that I created). Is there a command to do that? I currently use .exit followed by node, but I’d like to do it without closing and reopening the REPL, and the .clear command does not seem to be working.


Solution

  • How about creating a new REPLServer instance?

    $ node -r repl -e "repl.start()"
    > const x = 'hello';
    undefined
    > .clear
    Clearing context...
    > x
    Uncaught ReferenceError: x is not defined
    

    REPLServer is customizable. See this reference: https://nodejs.org/api/repl.html .