0

I don't have any background with angular or typescript. I'm a Data Scientist but currently we have to write a code in typescript that writes something into a file everytime a function is called. I followed the Tour of Heroes tutorial just for testing (https://angular.io/tutorial) and then I tried to create this:

var fName = arguments.callee.toString().match(/function ([^\(]+)/)[1];

var currentdate = new Date();
var datetime = 'Last Sync: ' + currentdate.getDate() + '/'
    + (currentdate.getMonth()+1) + '/'
    + currentdate.getFullYear() + ' @ '
    + currentdate.getHours() + ':'
    + currentdate.getMinutes() + ':'
    + currentdate.getSeconds() + ':'
    + currentdate.getMilliseconds();

const fs = require('fs');

fs.appendFile('logs.txt', 'C1 F0 ' + datetime, function (err) {
    if (err) throw err;
});

But this gives me an error saying Can't resolve 'fs'

I've already seen that it's normal because it's a client-side framework. So any workarounds or any solutions?

Thanks in advance and sorry if I said something wrong.

enno.void
  • 6,242
  • 4
  • 25
  • 42
  • 1
    If you want to write a file you have to do this from `node`, not within the browser (with angular or other framework). – maxime1992 Jan 27 '20 at 15:17
  • 1
    `fs` is a NodeJS (server-side Javascript) library, Where do you want to write your file ? You might have to use something other than TS and angular – Nicolas Jan 27 '20 at 15:17
  • @maxime1992 what do you mean with doing this from the node? –  Jan 27 '20 at 15:18
  • @Nicolas basically we want to write a local file with information everytime the function is called –  Jan 27 '20 at 15:18
  • @ShabbirMomin Only server-side code can write a file on the server. You cannot do this from your browser. –  Jan 27 '20 at 15:20
  • @ShabbirMomin Then you don't have much choice. You could make yourself a basic web API that could write to a file, since it's serverside. Your frontend would then call this API when it need to write to a file. – Nicolas Jan 27 '20 at 15:23
  • 1
    Thank you all for your answers, I will see with the team a solution that is within our area of expertise. –  Jan 27 '20 at 15:29
  • You need more than just Angular for this. As others have said, you can't write to the file system from the browser. If you're trying to make a (desktop) app, you should look into Electron, and if you like Angular (i know you said you're new to it), you should look into angular-electron. Electron is used to create cross platform desktop apps using web tech. – Chris Newman Jan 27 '20 at 15:31
  • I was talking about NodeJS https://nodejs.org/en – maxime1992 Jan 27 '20 at 15:49

0 Answers0