Basic file ops: Deno’s equivalent of Node.js
This is a part of the series: Deno’s equivalent of Node.js. In the brief articles that are part of this series, we explore the Deno’s equivalent of commonly used functions in Node.js. The articles are written in no particular order.
In this article, we’ll see the Deno’s equivalent of Node.js’s basic file ops like:
- Opening a file
- Reading from a file
- Writing to a file
- Appending to a file
- Truncating a file
- Closing a file
For simplicity, only async variants would be shown.
File Ops
For each file op, Node.js has callback based functions (old style) and async/sync functions (new style). Deno only has async/sync functions.
Imports
- In Node.js, the core fs module needs to be imported
- In Deno, the read/write functions are part of the core runtime.
//Node.js
const fs=require('fs');
const fsp=require('fs').promises;//Deno
//No imports required
Opening a file
- In Node.js, fs.open & fsp.open is used to open a file
- In Deno, Deno.open is used to open a file
//Node.js - callback
fs.open('demo.txt', 'r', (err, fd) => {
//fd is file descriptor
});//Node.js - promise
const file = await fsPromises.open('demo.txt');
//file is file handle object//Deno
const file = await Deno.open('demo.txt');
//file is file handle object
Reading some data from an open file
- In Node.js, fs.read can be used to read from an open file descriptor & fileHandle.read can be used to read from a file object. There is no promise package level read function.
- In Deno, file.read or Deno.read (takes rid as input) can be used to read from a file
const buf=new Uint8Array(1000);//Node.js - callback
fs.read(fd, buf, 0, 1000, (err, numBytes) => {});//Node.js - promise
const numBytes=file.read(buf, 0, 1000);//Deno
const numBytes=await file.read(buf);
const numBytes=await Deno.read(file.rid, buf);
Reading entire file
- In Node.js, fs.readFile, fileHandle.readFile & fsp.readFile can be used to read the entire file into a buffer. If encoding is specified, string would be returned, otherwise buffer would be returned.
- In Deno, readFile or readTextFile can be used to read the entire file into a buffer or a string, respectively.
In both runtimes, these functions take care of opening, reading, and closing of the file.
//Node.js - callback
fs.readFile('demo.txt', (err, data) => {});
fs.readFile('demo.txt', 'utf8', (err, str) => {});//Node.js - promise
const buf=await fsp.readFile('demo.txt');
const str=await fsp.readFile('demo.txt', 'utf8');//Deno
const buf=await Deno.readFile('demo.txt');
const str=await Deno.readTextFile('demo.txt');
Writing some data into an open file
- In Node.js fs.write, fileHandle.write can be used to write a buffer into an open file
- In Deno, file.write & Deno.write can be used to write a buffer into an open file
const buf=new Uint8Array(1000).fill(65);//Node.js - callback
fs.write(fd, buf, (err, numBytes) => {});//Node.js - promise
const numBytes=file.write(buf);//Deno
const numBytes=await file.write(buf);
const numBytes=await Deno.write(file.rid, buf);
Appending data to a file
- In Node.js, fs.appendFile & fsp.appendFile can be used to append some data to a file. This function takes care of opening, writing, and closing of the file.
- In Deno, the same Deno.writeFile & writeTextFile function with an extra option can be used to append data to a file
const str='data to append';
const data=new Uint8Array(100).fill(65);//Node.js - callback
fs.appendFile('demo.txt', data, err => {});
fs.appendFile('demo.txt', str, err => {});//Node.js promise
await fsp.appendFile('demo.txt', data);
await fsp.appendFile('demo.txt', str);//Deno
await Deno.writeFile('demo.txt', data, {append: true});
await Deno.writeTextFile('demo.txt', str, {append: true});
Truncating a file
- In Node.js, fs.truncate, file.truncate, & fsp.truncate can be used to truncate a file
- In Deno, file.truncate & Deno.truncate can be used to truncate a file
In both runtimes, an optional length can be provided to truncate from.
//Node.js - callback
fs.truncateFile('demo.txt', err => {});
fs.truncateFile('demo.txt', 10, err => {});//Node.js - promise
await fsp.truncateFile('demo.txt');
await fsp.truncateFile('demo.txt', 10);await file.truncate();
await file.truncate(10);//Deno
await Deno.truncate('demo.txt');
await Deno.truncate('demo.txt', 10);await file.truncate();
await file.truncate(10);
Closing a file
- In Node.js, fs.close & file.close can be used to close an open file. There is no such function in the promises package.
- In Deno, Deno.close & file.close can be used to close an open file.
//Node.js - callback
fs.close(fd, err => {});//Node.js - promise
await file.close();//Deno
Deno.close(file.rid);
file.close();
This story is a part of the exclusive medium publication on Deno: Deno World.