The JS runtimes
Published in

The JS runtimes

Going from Node.js to Deno: Part 8— Read/write files

Node.js is a 13-year-old, very popular server side runtime that runs JavaScript code in the backend. Deno is a 4-year-old, relatively new runtime that runs JavaScript and TypeScript code in the backend. There are similarities and differences between Deno and Node.js.

Considering the extreme popularity of Node.js and the fact that Deno is slowly gaining grounds, in the near future, many developers may need to transition from Node.js to Deno, or work on both the technologies together. Either way, there will be a need to learn or transition to Deno.

In this series of articles (part 1, part 2, part 3, part 4, part 5, part 6, part 7, part 9), we’ll provide a comprehensive guide for Node.js developers who are interested in learning and working on Deno.

Reading binary file

Node.js’s core fs module comes with a readFile API that can be used to read different types of files. If no options are specified, the file is read as a Node.js buffer.

A quick run:

Deno comes with a core API readFile that can be used to read any file as a Uint8Array buffer. There is a different API for text files.

A quick run:

Reading text file

The same readFile API with encoding option set to ‘utf-8’ can be used to read the file as a string.

A quick run:

Deno has a different API readTextFile that needs to be used to read a file as a string.

A quick run:

Reading JSON file

Node.js doesn’t have native support for reading JSON files. The file needs to be read as text and then parsed into JavaScript object with JSON.parse.

A quick run:

Like Node.js, Deno doesn’t have native JSON support, too. Like Node.js, the file needs to be parsed manually.

A quick run:

Write binary file

Node.js’s core fs module comes with a writeFile API that can be used to write different types of files. A Node.js buffer can be passed to write any kind of binary data.

A quick run:

Deno comes with a core API writeFile that can be used to write any Uint8Array buffer into a file. There is a different API for text files.

A quick run:

Write text file

The same writeFile API can be used to write a text file by passing a string as input.

A quick run:

Deno comes with a core API writeTextFile that can be used to write any string into a file.

A quick run:

Write JSON file

Node.js doesn’t have native support for writing JavaScript object into files (JSON). The object needs to be serialized using JSON.stringify.

A quick run:

Like Node.js, Deno doesn’t have native support for writing JavaScript object into files (JSON). too. Like Node.js, the data needs to be serialized using JSON.stringify.

A quick run:

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