I don't think this is possible, at least not exactly how you describe it.
The key blocker is that there is no way to get a list of all currently-visible variables, or their values.
So at best you'd have to change the scenario a bit: start by reading a file (or other string) containing the source code you want to process. Then use a JavaScript parser (e.g. Esprima; no endorsement, just illustration) to get an AST (abstract syntax tree). Then you can do whatever transformations you like on that AST (such as: replacing constant variables with their value), and then convert the AST back to a string (which you can then write to a file, if desired).
The above should work for simple cases like the example you gave. When you look at more complicated scenarios, you'll run into obstacles and limitations though. For example:
const x = 5;
function Case1() {
let y = x;
if (someCondition()) y++;
console.log(y); // what now?
}
function Case2() {
for (let i = 0; i < x; i++) {
console.log(i); // what now?
}
}
function Case3() {
const y = {foo: 42, valueOf() { return "null"; }};
console.log(y); // what now?
}
function Case4() {
const y = Math.random() > 0.5 ? "yes" : "no";
console.log(y); // what now?
}
I'm sure there are plenty more situations where this idea is difficult to put into practice.
You didn't say what your ultimate goal is, and this does have the smell of an xy question. Are you looking for optimization opportunities? A debugging environment? I strongly suspect that there's an easier way to accomplish whatever your actual goal is.
(Also, this doesn't have anything to do with V8, libuv, or Node.js; I'll drop tags accordingly.)