node.js是否可以启动脚本,等待其完成(或出现错误(,然后跳到控制台,访问当前环境变量?
一般使用python时,可以添加-i标志来实现这一点。node是否存在等价项?如果是,怎么做?
您可以在代码末尾使用repl.start()
来完成此操作。
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;
请注意,默认情况下,REPL中只有全局变量可访问。如果要访问其他变量,必须通过context
传递它们
repl.start().context.yourVariable
在NodeJS文档中阅读更多信息。