0

I'm trying to print the result of my console.log to another .txt file, how would I make this happen? this is what my console.log looks like this: https://i.stack.imgur.com/pYY00.jpg

I'm wanting this printed as regular text to an output file e.g 'output.txt'

Aidan el Goste
  • 383
  • 1
  • 3
  • 23
  • You could do basically this: https://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server – Austin T French Nov 22 '17 at 03:36
  • @AustinFrench I'm not using any html in my program - i want to type something into my terminal and have it output to another file – Aidan el Goste Nov 22 '17 at 03:43
  • I get that, So I don't see this as a duplicate but the concept would be the same: Allocate memory, out stuff in it, open a stream and download. I don't have the energy right now to hack it, so perhaps you can or another answerer will – Austin T French Nov 22 '17 at 03:45
  • thats fine, thanks anyways @AustinFrench – Aidan el Goste Nov 22 '17 at 03:46

3 Answers3

1

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');
Vitaly Migunov
  • 4,297
  • 2
  • 19
  • 23
0

Can you rewrite your program as a node app and then just run it from the Terminal window instead? Then, you wouldn't need to wrestle with this problem of getting your console output into a file... you could use node's fs instead (Node.js Write a line into a .txt file)

siege_Perilous
  • 1,109
  • 9
  • 8
  • It is a node app, and I am already using node's fs already. I'm asking how would I use it to log it to another file. the question you linked asked how to send a string to another file, I want to send a console.log's results – Aidan el Goste Nov 23 '17 at 03:11
0

This answer works, but it isn't what i'm aiming for it will work for most other people i'd assume. you're gonna want to run your program like so:

npm start > output.txt

this will print your console output to the output.txt file I still need to achieve this with node fs so i'd appreciate anymore answers

Aidan el Goste
  • 383
  • 1
  • 3
  • 23