fs.readFile(), fs.readFileSync() and also Amazon S3 return a Buffer object. How can I use this Buffer as actual JavaScript code that I can execute?
Here is a very basic example.
some-file.js:
module.exports = function () {
console.log('hello');
};
main.js
const data1 = fs.readFileSync('./some-file.js'); // This is a Buffer
const data2 = fs.readFileSync('./some-file.js', 'utf-8'); // This is a string
data1(); // Error: data1 is not a function
JSON.parse(data2)(); // Error: data2 is no valid JSON