Node JS Tutorial for Beginners #9 - Reading & Writing Files (fs)

preview_player
Показать описание
Yo ninjas, in this Node JS tutorial, I'll go through how we can use the 'fs' module in node to go out and read & write files to our computer.

----- COURSE LINKS:

---------------------------------------------------------------------------------------------
You can find more front-end development tutorials on CSS, HTML, JavaScript, jQuery, WordPress & more on the channel homepage...

========== JavaScript for Beginners Playlist ==========

============ CSS for Beginners Playlist =============

============== The Net Ninja =====================

================== Social Links ==================

Рекомендации по теме
Комментарии
Автор

Now if we only use fs.writeFile('file.txt', data) it won't work, we have to use callback function after data like this: fs.writeFile('file.txt, data, (err)=>{ }) so that it will be worked !!!

angphu
Автор

For the last segment
fs.writeFile('writeme.txt', data, function (err, _data){});

As writeFile is asynchronous you have to define a callback function, doesn't matter if it's empty, it will still run with no syntax error and get the job done.
Or you can simply use writeFileSync()
Hope this helps everyone.

shivendrasrivastava
Автор

Awesome, finlay understood syncronous and asyncronous.

rucklerful
Автор

Great job. I learned more from this than I had in the preceding 3 hours.

escapefelicity
Автор

I like the way you go through all the stuff, helps me understand better

MrToPi
Автор

I hope you'll be talking about database access with Node JS.

IslanKleinknecht
Автор

Wow! Finally able to understand synchronous and asynchronous.
Thanks a ton man!

prateekcool
Автор

The best way to learn Node JS is to watch your video series and read comments below of each video) Thanks)

ayseak_
Автор

if anyone gets the "TypeError [ERR_INVALID_CALLBACK]: Callback must be a function" error just add ()=>{} as a third argument to the writeFile function to fix it

georgephillips
Автор

Finally a very good node.js tutorial!! You're very good, man!

SilvestreVivo
Автор

Got this warning from node:
DeprecationWarning: Calling an asynchronous function without callback is deprecated.

I replaced

fs.writeFile("writeMe.txt", data);

with

fs.writeFile('writeMe.txt', data, (err) => {
if (err) throw err;
console.log('It is saved!');
});

Hope it helps.

johnsonlai
Автор

You didn't destroy my eardrums in this video, THANK YOU

JonathanSueck
Автор

episodes like this is when my brain starts running about thinking of all the awesome things you can do with this...

nahfamimgood
Автор

this is probably the best tutorial i've ever seen

matheussanchez
Автор

thank you so much, even in 2022 its very useful

shihabmunshi
Автор

Things are starting to clear up in my head. Another great tutorial.

tomaszkonecki
Автор

Excellent video and so well explained 👏 👍 👌 😀 thanks so much

mzamomahaeng
Автор

My god. Java programmer here . I wanted to table flip after I saw how easy it is to readfiles in Node js and I believe this feeling of how easy it is will come back again and again:D

vilmiswow
Автор

I showed this video to the interviewer and I'm the CEO of that company now. Thanks for sharing this amazing tutorial with us.

muhammadsafiullah
Автор

@8:40 - If you got an error like ERR_INVALID_CALLBACK: Callback must be a function
(possibly depreciation since 2016?)

This is referring to fs.writeFile(yourfilename, yourdata);
You can try to use:
async-function{ sync-function }
or
async-function{ async-function }

Example1: async-function{ sync-function }
fs.readFile('./readme.txt', 'utf8', function(err, data){
fs.writeFileSync('writeme.txt', data);
});

Example2: async-function{ async-function }
fs.readFile('./readme.txt', 'utf8', function(err, data){
fs.writeFile('writeme.txt', data, function(err2, data2){
console.log('Use this method if you want another callback function after you read the file, then write to a new file');
});
});

You can also throw in a "dummy" function. The error requests a callback, but you don't want it to do anything:
fs.readFile('readme.txt', 'utf8', function(err, data) {
fs.writeFile('writeme.txt', data, function());
})

There are probably a couple of other options, but these just popped into my head

PixiiBomb