Answers for "how to open a file with node.js"

3

node open file

const fs = require("fs");

// __dirname means relative to script. Use "./data.txt" if you want it relative to execution path.
fs.readFile(__dirname + "/data.txt", (error, data) => {
    if(error) {
        throw error;
    }
    console.log(data.toString());
});
Posted by: Guest on May-25-2020
2

read a file nodejs

const fs = require('fs');

fs.readFile('my-file.txt', 'utf8', function(err, data) {
    if (err) throw err;
    console.log(data);
});
Posted by: Guest on April-17-2021

Code answers related to "how to open a file with node.js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language