Home > database >  How to jump into REPL after the execution of a node.js script?
How to jump into REPL after the execution of a node.js script?

Time:04-10

Is it possible with node.js to launch a script, wait for its completion (or an error), and then jump into the console, with access to the current environment variables?

When using python in general, you can add the -i flag to achieve this. Does the equivalent exist with node? If yes, how to do it?

CodePudding user response:

You can use repl.start() at the end of your code to do that.

const repl = require("repl");

const myVariable = 1;
console.log("myVariable: ", myVariable);
// provide extra contexts here to access in the REPL
repl.start().context.myVariable = myVariable;

Note that by default only global variables are accessible in the REPL. If you want to access other variables, you must pass them via context

repl.start().context.yourVariable

Read more in the NodeJS document.

  • Related