A Deno Guide for Node.js Developers

A Deno Guide for Node.js Developers

If you come from Node.js, you might find that a lot of things are very similar in Deno, here we show some features that Deno and Node.js have in common, it would be great for learning purpose.

Node vs Deno

If you come from Node.js, you might find that a lot of things are very similar in Deno, here we show some features that Deno and Node.js have in common, it would be great for learning purpose.

Keep updating..

Table of Contents

Built-in features

In Node.js, most built-in features are exposed as CommonJS modules which you can use via require calls, while in Deno, they are exposed on the global namespace Deno.

For example, in Node.js you use require('child_process').spawn to start a subprocess, in Deno you can use Deno.run instead.

For list of all available features go to Deno API Reference.

Check if it's running in Deno

Due to the fact that Deno exposes its specific features on Deno namespace, you can use it to determine if your program is running in Deno:

const isDeno = typeof window !== 'undefined' && window.Deno

Command-line arguments

process.argv represents command line arguments in Node.js, run the example code:

run node example

The first item is the name of the program being executed, in our case, it's an absolute path to node.

In Deno, you use Deno.args instead to retrive command line arguments:

run deno example

Note that there're only three items in the array, the path to deno executable is not included. To get path to Deno executable use Deno.execPath(). The first item is also just a relative path.

Spawn a subprocess

Node.js's child_process.spawn() method is used to spawn a new process:

const { spawn } = require('child_process')

const p = spawn('ls', ['./examples'])

p.on('close', code => {
  // completed with status `code`
})

p.stdout.on('data', data => {
  process.stdout.write(data)
})

p.stderr.on('data', data => {
  process.stderr.write(data)
})

Node.js by default creates a pipe between the child process and the parent process for parent-child communication.

The Deno equivalent is Deno.run:

const p = Deno.run({
  args: ['ls', './examples']
})

// await its completion
const code = await p.status()

In Deno the subprocess is inherited from parent process by default, if you want it to work like the default behavior in Node.js you can use piped option:

const p = Deno.run({
  args: ['ls', './examples'],
  stdout: 'piped',
  stderr: 'piped'
})

const code = await p.status()

if (code === 0) {
  const rawOutput = await p.output()
  await Deno.stdout.write(rawOutput)
} else {
  const rawError = await p.stderrOutput()
  await Deno.stderr.write(rawError)
}

Hashing algorithms

In Node.js:

const crypto = require('crypto')

// sha1
console.log(
  crypto
    .createHash('sha1')
    .update('hello world')
    .digest('hex')
)

// md5
console.log(
  crypto
    .createHash('md5')
    .update('hello world')
    .digest('hex')
)

In Deno:

import { Hash, encode } from 'https://deno.land/x/checksum/mod.ts'

// sha1
console.log(new Hash('sha1').digest(encode('hello world')).hex())

// md5
console.log(new Hash('md5').digest(encode('hello world')).hex())

Download Details:

Author: egoist

Source Code: https://github.com/egoist/node-vs-deno

deno nodejs node javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

AEM Querybuilder for JavaScript (Browser, Node, Deno)

Helix Querybuilder .AEM Querybuilder for JavaScript (Browser, Node, Deno)

NKeys for JavaScript - Node.js, Browsers, and Deno

A public-key signature system based on Ed25519 for the NATS ecosystem system for JavaScript. The nkeys.js library works in Deno, Node.js, and the browser!

Javascript module for easily generating mazes,compatible with both Deno and Node

A work-in-progress Javascript maze generator module, compatible with both Deno and Node.

Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno is released, it’s time to find out if it can prove a worthy competition for Node.js. What is Deno? Why Deno can be considered an alternative to Node? The main differences between Deno and Node. Write a simple Deno app.

What is Deno? The Node.JS Killer? The Deno-minator to Node?

What is Deno? The Node.JS Killer? The Deno-minator to Node? Deno is the new hype and will it be better than node.js? Deno is a secure runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language that addresses Node’s shortcomings