Creating and Opening Files in Node.js


Node.js is a widely used open-source JavaScript run-time environment capable of executing JavaScript code without the use of a browser. In this tutorial we will be exploring how to use Node.js methods to open/create files both asynchronously and synchronously.


fs.open(path[, flags[, mode]], callback)

fs.open is the asynchronous file open method in Node.js.

This method has four possible arguments with two of them being necessary for successfully opening a file.

The first argument is path. This is where the file path must be provided to the method either as a string, buffer, or URL type.

The second argument, flag, is used to provide additional functionality upon opening the file. This argument accepts a string or number type and is optional. By default, this is set to ‘r’ which opens the file for reading and appending while also creating the file if it does not exist. For a complete list of flags used by Node.js file system please reference the following documentation.

The third argument in fs.open() is mode which takes an integer type as data. This sets the permissions for the specified file if it was created by the fs.open function call. By default, mode is set to ‘0o666’ which allowed both read and write functionality for the newly created file.

Lastly, fs.open() comes with a callback function argument. This callback function gets two arguments, ‘err’, and ‘fd’. The ‘err’ callback argument is used to check if an error occurred and then display the error data if true. The second argument, ‘fd’, stands for file descriptor and contains the data relating to the newly created file.

Callbacks are an essential part of Node.js and must be used to execute subsequent code only once events have completed successfully (i.e the file has been successfully created).

fs.openSync(path[, flags, mode])

This method is similar to fs.open() but does not contain a callback function as an argument due to being synchronous.

The path, flags, and mode arguments are identical to the asynchronous version of the method.

Unlike fs.open(), fs.openSync() returns an integer representing the file descriptor. This is similar to the callback function’s ‘fd’ argument which returns data relating to the newly created file.

fs.appendFile(path, data,[, options], callback)

This asynchronous method appends user specified content to the end of a file. If the file does not exist, the method will create the file.

Similarly to fs.open(), this method takes a path argument of type string, buffer, URL, or number corresponding to the filename or file descriptor.

The data argument takes either a string or a buffer type and is used to provide the data that will be appended to the file.

Options is an argument that provides the encoding, mode, or flags for the file. If specifying encoding, a string or null type will be used or the file will default to ‘utf8’ encoding. Modes and flags will use the same data types and identifiers as the ones in the fs.open() method.

Finally, the callback function works the same as in fs.open() by taking an ‘err’ argument in order to provide error handling.

This method also has a synchronous counterpart:

fs.appendFileSync(path, data[,options]).

This method does not contain an argument to a callback function due to its synchronous nature, similar to fs.open() and fs.openSync().

All other functionality remains the same as its asynchronous counterpart, fs.appendFile().


Examples

var fs = require(‘fs’);

//Necessary in order to include and access fs methods

fs.open(‘newfile.txt’, ‘w’, function (err, fd) {

if (err) throw err;

//If an error occurred, throw error and exit method

console.log(‘File created successfully!’);

});

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

var fs = require(‘fs’);

//Necessary in order to include and access fs methods

fs.appendFile(‘newfile.txt’, ‘Write this at the end!’, function(err) {

if (err) throw err;

//If an error occurred, throw error and exit method

console.log(‘File successfully appended!’);

});