generate a js file dynamically using fs node

Solution 1:

You can run the code as a string by creating a function from it using the Function constructor.

const code = "let x = 100; console.log(x + 1)";  
const run = new Function(code);  
run();

Write this string to the JS file and run the js file using child process.