'How to jump into REPL after the execution of a node.js script?
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?
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Cuong Vu |