Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/egoist/node-vs-deno
A Deno guide for Node.js developers
https://github.com/egoist/node-vs-deno
Last synced: 2 days ago
JSON representation
A Deno guide for Node.js developers
- Host: GitHub
- URL: https://github.com/egoist/node-vs-deno
- Owner: egoist
- Created: 2019-09-11T09:46:56.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-01-14T06:38:27.000Z (almost 3 years ago)
- Last Synced: 2024-10-22T19:15:32.252Z (about 2 months ago)
- Size: 32.2 KB
- Stars: 358
- Watchers: 16
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-github-star - node-vs-deno
README
# Node vs Deno
If you come from [Node.js](https://nodejs.org), you might find that a lot of things are very similar in [Deno](https://deno.land), 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](#built-in-features)
- [Check if it's running in Deno](#check-if-its-running-in-deno)
- [Command-line arguments](#command-line-arguments)
- [Spawn a subprocess](#spawn-a-subprocess)
- [Hashing algorithms](#hashing-algorithms)
- [Colored output](#colored-output)## 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](https://deno.land/typedoc/).
## 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:
```ts
const isDeno = typeof window !== 'undefined' && window.Deno
```## Command-line arguments
`process.argv` represents command line arguments in Node.js, run the example code:
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:
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()`](https://deno.land/typedoc/index.html#execpath). The first item is also just a relative path.
## Spawn a subprocess
Node.js's [`child_process.spawn()`](https://nodejs.org/dist/latest/docs/api/child_process.html#child_process_child_process_spawn_command_args_options) method is used to spawn a new process:
```js
import { spawn } from '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`](https://deno.land/typedoc/index.html#run):
```ts
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:
```ts
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:
```js
import crypto from '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:
```js
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())
```## Colored output
In Node.js:
```js
import chalk from 'chalk'console.log(chalk.bold(chalk.bgGreen('foo')))
```In Deno:
```js
console.log('%cfoo', 'font-weight:bold;background-color:green')
```