I recommend to use Winston to achieve this.
You can set up Winston Transports to output in file winston.add(winston.transports.File, options)
Or if you don't wanna add any npm modules to your app you can just do this
var fs = require('fs');
module.exports = function(text) {
fs.appendFile('output.txt', text + '\n', function (err) {
if (err) throw err;
});
};
And save this to a file in your project directory, for example NameOfYourFile.js.
Then you can just require it in a file that you wanna make output from
var loger = require('./NameOfYourFile');
loger('Logs');
loger('Output');
loger('Working');
And just use loger instead of console.log. You also can easily rename it.
TypeScript version
First, install node modules
npm install @types/node --save-dev
Then create a file for your module, for example, NameOfYourFile.ts
import * as fs from 'fs';
export default function(text) {
fs.appendFile('output.txt', text + '\n', function (err) {
if (err) throw err;
});
};
Then you can import it like this
import loger from './NameOfYourFile';
loger('Logs');
loger('Output');
loger('Working');