execute some code and then go into interactive node

Really old question but…

I was looking for something similar, I believe, and found out this.
You can open the REPL (typing node on your terminal) and then load a file.
Like this: .load ./script.js.
Press enter and the file content will be executed. Now everything created (object, variable, function) in your script will be available.

For example:

// script.js
var y = {
    name: 'obj',
    status: true
};

var x = setInterval(function () {
    console.log('As time goes by...');
}, 5000);

On the REPL:

//REPL
.load ./script.js

Now you type on the REPL and interact with the “living code”.
You can console.log(y) or clearInterval(x);

It will be a bit odd, cause “As time goes by…” keep showing up every five seconds (or so).
But it will work!

Leave a Comment