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:
+ Repo –
+ Atom editor –
———————————————————————————————
You can find more front-end development tutorials on CSS, HTML, JavaScript, jQuery, WordPress & more on the channel homepage…
SUBSCRIBE TO CHANNEL –
========== JavaScript for Beginners Playlist ==========
============ CSS for Beginners Playlist =============
============== The Net Ninja =====================
For more front-end development tutorials & to black-belt your coding skills, head over to – or
================== Social Links ==================
Twitter – @TheNetNinja –
Nguồn: https://canhosafira.com.vn
Xem thêm bài viết khác: https://canhosafira.com.vn/cong-nghe
does this method of using async work with the latest version of node js because idk it's not working for me the same command.help!!!!
you are the best
Warning, if you are using the output on visual studio it won't work you need to use the terminal
Great job. I learned more from this than I had in the preceding 3 hours.
After Data add a Parameter err => {} it will work
Thank You
New Node version solution
const fs = require('fs')
fs.readFile('readMe.txt','utf8', (err, data) => {
fs.writeFile('writesMe.txt', data,(err,result) => {
if(err){
console.log(err);
}
})
})
Hey if anyone stuck upon the error like- call back must be function, then below is the given link go check it out.
https://stackoverflow.com/questions/51150956/how-to-fix-this-error-typeerror-err-invalid-callback-callback-must-be-a-funct
Any idea for this error? TIA
fs.js:143
throw new ERR_INVALID_CALLBACK(cb);
^
this is what my code following your example look like:
var fs = require('fs');
fs.readFile('readme.txt','utf8', function(err, data){
fs.writeFile('writeme.txt', data)
}); /*I have filenames in smallcaps*/
I understand everything you coded but in JavaScript we pass the arguments in function but in callback mentioned above function (err, data) {….. }
Where does the values for err, data comes from can you clear my doubt sir
What is your text editor
someone can help me??? is necesary the installation of react native fs ???? and how to do it
Great stoaf in these videos 🙂
The best way to learn Node JS is to watch your video series and read comments below of each video) Thanks)
such a great teacher and programmer…. 10/10
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.
I showed this video to the interviewer and I'm the CEO of that company now. Thanks for sharing this amazing tutorial with us.
@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
Thanks a lot for this useful and simple video <3
I hope writing text and retrieving text to and from an html file will be discussed
I don't know why fs module doesn't found my .txt file at the same folder. I need to concat the __dirname with '/readme.txt' string, so it's can run properly
is there any way to decompress zip file and store extracted files in some directory (other than adm-zip)?
Loved this video
And can i write a value to txt?
thank you)))))
I keep getting Error: ENOTDIR: not a directory, scandir 'readme.txt'
this is probably the best tutorial i've ever seen
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 !!!
how to store the read content in any variable in asynchronous function
Who knows what is wrong with this code:
let events = require("events");
let util = require("util");
let Person = (name) => {
this.name = name;
};
util.inherits(Person, events.EventEmitter);
let james = new Person('james');
let mary = new Person('mary');
let ruy = new Person('ruy');
let people = [james, mary, ruy];
people.forEach(person => {
person.on('speak', msg => {
console.log(person.name + ' said : ' + msg);
});
});
james.emit('speak', 'hey dudes');
Everything is identical as in video. Can it be because I use a newer version of Node JS?
you are a really Ninja 🐱👤🐱👤🐱👤
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
Good day sir. I always have an error with the 'fs.writeFile' but it works on 'fs.writeFileSync' and it also works on 'fs.readFile' . what do you think is my problem sir? thank you for answering. more power. Godbless.
P.S: this is a great Node Js tutorial .
Noted:
The asynchronous function always needs a callback function
Ninja code:
fs.readFile('readMe.txt', 'utf-8', function(err, data){
fs.writeFile('writeMe.txt', data);
});
But, I will get the below errror:
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
My code:
fs.readFile('readMe.txt', 'utf-8', function(err, data){
fs.writeFile('writeMe.txt', data, function(err, result){
if(err) console.log('error', err);
});
});
This will only works and hope can help you guys.
You are super amazing…..what is the name of the theme that you are using in atom…??
if you encounter an error said "Callback must be a function", add a function as the last parameter of fs.writeFiles() method.
For example:
const fs = require('fs');
fs.readFile('readme.txt', 'utf8', function(err, data) {
fs.writeFile('writeme.txt', data, function(){});
});
Or, if you want to keep it clear and simple, use this one:
const fs = require('fs');
fs.readFile('readme.txt', 'utf8', function(err, data) {
fs.writeFile('writeme.txt', data, ()=>{});
});
how u add the node.js console to atom ?? anyone know ? It is pretty cool and handy.
you have to add err callback function for WriteFile otherwise it is givng error.
var fs = require('fs');
fs.readFile('readMe.txt','utf8', function(err,data){
console.log('read complete');
fs.writeFile('writeMe.txt',data, function(err){
console.log('write complete');
});
});
Do you have any tutorials for intermediate/Advanced learners?
fs.readFile('readMe.txt', 'utf8', function (err, data)
{
fs.writeFile('writeMe.txt', data, function(err, result)
{
if(err) console.log('error', err);
});
});
correct code for async method of writing a file !!
The real question is, how is Ninja not getting an error from running that last bit of code?
New version of nodeJS requires a callback function in async methods..
var fs = require('fs');
fs.readFile('readme.txt','utf8', function(err, data){
fs.writeFile('writeme.txt',data,function(err){
if(err)
console.log(err);
});
});
Hi, thanks for this video and others. Just one thing (maybe more for the others): now writeFile does not work without a callback function. Node.js returns ERR_INVALID_CALLBACK error. The definition of a callback seems mandatory.
Fire!
your tutorials allowed me to achieve procedural code recombination (an early version of evolution code). thanks