The JS runtimes
Published in

The JS runtimes

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:

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

//Node.js
const fs=require('fs');
const fsp=require('fs').promises;
//Deno
//No imports required

Opening 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

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 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

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

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 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

//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.

--

--

Articles on the popular JS runtimes, Node.js, Deno, and Bun

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store