Tutorial by Examples

var fs = require('fs'); // Save the string "Hello world!" in a file called "hello.txt" in // the directory "/tmp" using the default encoding (utf8). // This operation will be completed in background and the callback // will be called when it is either done or fail...
Use the filesystem module for all file operations: const fs = require('fs'); With Encoding In this example, read hello.txt from the directory /tmp. This operation will be completed in the background and the callback occurs on completion or failure: fs.readFile('/tmp/hello.txt', { encoding: '...
const fs = require('fs'); // Read the contents of the directory /usr/local/bin asynchronously. // The callback will be invoked once the operation has either completed // or failed. fs.readdir('/usr/local/bin', (err, files) => { // On error, show it and return if(err) return console.er...
For any file operations, you will need the filesystem module: const fs = require('fs'); Reading a String fs.readFileSync behaves similarly to fs.readFile, but does not take a callback as it completes synchronously and therefore blocks the main thread. Most node.js developers prefer the asynch...
Delete a file asynchronously: var fs = require('fs'); fs.unlink('/path/to/file.txt', function(err) { if (err) throw err; console.log('file deleted'); }); You can also delete it synchronously*: var fs = require('fs'); fs.unlinkSync('/path/to/file.txt'); console.log('file deleted'...
While reading content from a file is already asynchronous using the fs.readFile() method, sometimes we want to get the data in a Stream versus in a simple callback. This allows us to pipe this data to other locations or to process it as it comes in versus all at once at the end. const fs = require(...
fs.access() determines whether a path exists and what permissions a user has to the file or directory at that path. fs.access doesn't return a result rather, if it doesn't return an error, the path exists and the user has the desired permissions. The permission modes are available as a property on ...
Due to Node's asynchronous nature, creating or using a directory by first: checking for its existence with fs.stat(), then creating or using it depending of the results of the existence check, can lead to a race condition if the folder is created between the time of the check and the time of ...
Asynchronously var fs = require('fs'); fs.stat('path/to/file', function(err) { if (!err) { console.log('file or directory exists'); } else if (err.code === 'ENOENT') { console.log('file or directory does not exist'); } }); Synchronously here, we must wr...
This program illustrates how one can copy a file using readable and writable streams using the createReadStream(), and createWriteStream() functions provided by the file system module. //Require the file System module var fs = require('fs'); /* Create readable stream to file in current direc...
This program copies a file using readable and a writable stream with the pipe() function provided by the stream class // require the file system module var fs = require('fs'); /* Create readable stream to file in current directory named 'node.txt' Use utf8 encoding Read the data...
Example. It will be replacing the word email to a name in a text file index.txt with simple RegExp replace(/email/gim, 'name') var fs = require('fs'); fs.readFile('index.txt', 'utf-8', function(err, data) { if (err) throw err; var newValue = data.replace(/email/gim, 'name'); ...
app.js const readline = require('readline'); const fs = require('fs'); var file = 'path.to.file'; var linesCount = 0; var rl = readline.createInterface({ input: fs.createReadStream(file), output: process.stdout, terminal: false }); rl.on('line', function (line) { linesCo...
app.js const readline = require('readline'); const fs = require('fs'); var file = 'path.to.file'; var rl = readline.createInterface({ input: fs.createReadStream(file), output: process.stdout, terminal: false }); rl.on('line', function (line) { console.log(line) // print...

Page 1 of 1