Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn a file into a string in NodeJS [duplicate]

I am attempting to convert a text file (on my machine) into a string. What is the best/simplest way to do this? I am looking for a basic function that would look like this:

function fileToString(filepath) {
   //this returns a string with the contents of the file
}

How should I do this?

EDIT: I now know that there is another question that asks this, but I didn't understand that question so I asked it in different words.

like image 635
Moish Avatar asked Dec 28 '18 06:12

Moish


1 Answers

You need node.js for that and this code:

const fs = require('fs')

const fileContents = fs.readFileSync('./myFile').toString()
like image 86
Nurbol Alpysbayev Avatar answered Sep 16 '22 15:09

Nurbol Alpysbayev