Skip to content
Sign upLog in

JavaScript fs.readFile

Profile icon
ajayprashar

Hi.

My name is Ajay and I am learning JavaScript using Repl.it.

When I use the code below I receive the error that 'fs.readFile' is NOT a function. Does anyone know how to use this function within repl.it?

import fs from 'fs'; let ourStuff = fs.readFile('G:\DATA\git\ipromise\ipromise.txt','utf8', callback); console.log(ourStuff);

https://repl.it/@ajayprashar/iPromise

You are viewing a single comment. View All
Profile icon
ajayprashar

@TheDrone7
Hi! I wanted to bring this question to a close and thought I would provide an update.

I took your advice and modified the code to be more readable.

// Requiring fs module in which // readFile function is defined. const fs = require('fs'); // Reading data in utf-8 format // which is a type of character set. // Instead of 'utf-8' it can be // other character set also like 'ascii' fs.readFile('G:\\DATA\\git\\ipromise\\ipromise.txt', 'utf-8', (err, data) => { if (err) throw err; // Converting Raw Buffer to text // data using tostring function. console.log('#################'); console.log(data); })

I believe your comment regarding the first "const" line fixed a portion of it and I was also erroring on the fact that I need double backslashes "\" instead of single as singles were getting interpreted as escape sequences.

When I run the updated code on REPL.IT, it runs error free but with no output.

When I run the code locally using Node, it runs successfully with the expected output.

Is this normal? Security related?

-Ajay