Going from Node.js to Deno: Part 4 — Command-line args & user input

Mayank C
Tech Tonic

--

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 5, part 6, part 7, part 8, part 9), we’ll provide a comprehensive guide for Node.js developers who are interested in learning and working on Deno.

Command-line args

Node.js

Natively, Node.js makes all command-line args available in the process.argv object, which is a simple array of strings. Node.js does not provide any parsing of the command-line args. The first two strings in the argv array are ignored, as they contain the name of the executed and path of the application. Usually, process.argv.slice(2) is used to remove the unwanted args.

console.log(process.argv);// --> node app.mjs -a 1 -b 2 -c -d -e 
[
'/Users/mayankc/.nvm/versions/node/v18.4.0/bin/node',
'/Users/mayankc/Work/source/denoExamples/app.mjs',
'-a',
'1',
'-b',
'2',
'-c',
'-d',
'-e'
]

Deno

Natively, Deno also makes all the command-line args available in the Deno.args object, which is also a simple array of strings. Like Node.js, Deno doesn’t perform any kind of parsing on the command-line args. Unlike Node.js, the args array only contains the provided arguments. There is no need to slice.

console.log(Deno.args);// --> deno run app.ts -a 1 -b 2 -c -d -e 
[
"-a", "1", "-b",
"2", "-c", "-d",
"-e"
]

Processed command-line args

The native command-line arguments support is not of much use, as the burden of parsing is on the user. Fortunately, in both Node.js and Deno, command-line args can be processed through third-party packages.

Node.js

Node.js recommends the use of yargs module for processing of the command-line args. In their own words:

const yargs = require("yargs/yargs");
const args = yargs(process.argv.slice(2)).argv;
console.log(args);
// --> node app.js -a 1 -b 2 -c -d -e
{ _: [], a: 1, b: 2, c: true, d: true, e: true, '$0': 'app.js' }

Deno

Deno’s standard library’s flags module contains APIs (parse) to process command-line args. The usage & output of flags is quite similar to the usage and output of yargs. In their own words:

import { parse } from "https://deno.land/std/flags/mod.ts";
const args = parse(Deno.args);
console.log(args);
// --> deno run app.ts -a 1 -b 2 -c -d -e
{ _: [], a: 1, b: 2, c: true, d: true, e: true }

User input

Node.js

The native way of reading user input is through readline module that involves input & output stream handling. The code gets verbose and complex. Node.js recommends using prompt module for getting user inputs. The prompt module provides a very easy to use interface along with input validations, hiding password characters, etc.

import prompt from "prompt";prompt.start();const ipAddress = await prompt.get("IP Address");
console.log("Entered address:", ipAddress["IP Address"]);
// --> node app.mjs
prompt: IP Address: 1.1.1.1
Entered address: 1.1.1.1

Deno

Deno natively supports web standard prompt API for getting user inputs. The web standard prompt API doesn’t perform any input validations. However, there is a way to provide a default option in case the user chooses to skip entering the input.

const ipAddress = prompt("IP Address", "0.0.0.0");
console.log("Entered address:", ipAddress);
// --> deno run app.ts
IP Address [0.0.0.0] 1.1.1.1
Entered address: 1.1.1.1

--

--