{"id":13603031,"url":"https://github.com/egoist/node-vs-deno","last_synced_at":"2025-04-08T03:13:31.752Z","repository":{"id":41413237,"uuid":"207777781","full_name":"egoist/node-vs-deno","owner":"egoist","description":"A Deno guide for Node.js developers","archived":false,"fork":false,"pushed_at":"2022-01-14T06:38:27.000Z","size":33,"stargazers_count":358,"open_issues_count":3,"forks_count":3,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-29T17:42:26.274Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/egoist.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-11T09:46:56.000Z","updated_at":"2024-10-06T07:26:26.000Z","dependencies_parsed_at":"2022-09-21T08:11:42.589Z","dependency_job_id":null,"html_url":"https://github.com/egoist/node-vs-deno","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egoist%2Fnode-vs-deno","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egoist%2Fnode-vs-deno/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egoist%2Fnode-vs-deno/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/egoist%2Fnode-vs-deno/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/egoist","download_url":"https://codeload.github.com/egoist/node-vs-deno/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247767236,"owners_count":20992548,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T18:01:47.197Z","updated_at":"2025-04-08T03:13:31.721Z","avatar_url":"https://github.com/egoist.png","language":null,"readme":"# Node vs Deno\n\nIf 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.\n\nKeep updating..\n\n## Table of Contents\n\n\u003c!-- toc --\u003e\n\n- [Built-in features](#built-in-features)\n- [Check if it's running in Deno](#check-if-its-running-in-deno)\n- [Command-line arguments](#command-line-arguments)\n- [Spawn a subprocess](#spawn-a-subprocess)\n- [Hashing algorithms](#hashing-algorithms)\n- [Colored output](#colored-output)\n\n\u003c!-- tocstop --\u003e\n\n## Built-in features\n\nIn 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`.\n\nFor example, in Node.js you use `require('child_process').spawn` to start a subprocess, in Deno you can use `Deno.run` instead.\n\nFor list of all available features go to [Deno API Reference](https://deno.land/typedoc/).\n\n## Check if it's running in Deno\n\nDue 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:\n\n```ts\nconst isDeno = typeof window !== 'undefined' \u0026\u0026 window.Deno\n```\n\n## Command-line arguments\n\n`process.argv` represents command line arguments in Node.js, run the example code:\n\n\u003cp\u003e\n\u003ca href=\"./examples/args.js\"\u003e\n\u003cimg alt=\"run node example\" src=\"https://user-images.githubusercontent.com/8784712/64687080-68480000-d4bc-11e9-865e-a70f1b132a69.png\" width=\"100%\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\nThe first item is the name of the program being executed, in our case, it's an absolute path to `node`.\n\nIn Deno, you use `Deno.args` instead to retrive command line arguments:\n\n\u003cp\u003e\n\u003ca href=\"./examples/args.ts\"\u003e\n\u003cimg alt=\"run deno example\" src=\"https://user-images.githubusercontent.com/8784712/64687185-9a596200-d4bc-11e9-8cf7-6868b29fdde4.png\" width=\"100%\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\nNote 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.\n\n## Spawn a subprocess\n\nNode.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:\n\n```js\nimport { spawn } from 'child_process'\n\nconst p = spawn('ls', ['./examples'])\n\np.on('close', code =\u003e {\n  // completed with status `code`\n})\n\np.stdout.on('data', data =\u003e {\n  process.stdout.write(data)\n})\n\np.stderr.on('data', data =\u003e {\n  process.stderr.write(data)\n})\n```\n\nNode.js by default creates a pipe between the child process and the parent process for parent-child communication.\n\nThe Deno equivalent is [`Deno.run`](https://deno.land/typedoc/index.html#run):\n\n```ts\nconst p = Deno.run({\n  args: ['ls', './examples']\n})\n\n// await its completion\nconst code = await p.status()\n```\n\nIn 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:\n\n```ts\nconst p = Deno.run({\n  args: ['ls', './examples'],\n  stdout: 'piped',\n  stderr: 'piped'\n})\n\nconst code = await p.status()\n\nif (code === 0) {\n  const rawOutput = await p.output()\n  await Deno.stdout.write(rawOutput)\n} else {\n  const rawError = await p.stderrOutput()\n  await Deno.stderr.write(rawError)\n}\n```\n\n## Hashing algorithms\n\nIn Node.js:\n\n```js\nimport crypto from 'crypto'\n\n// sha1\nconsole.log(\n  crypto\n    .createHash('sha1')\n    .update('hello world')\n    .digest('hex')\n)\n\n// md5\nconsole.log(\n  crypto\n    .createHash('md5')\n    .update('hello world')\n    .digest('hex')\n)\n```\n\nIn Deno:\n\n```js\nimport { Hash, encode } from 'https://deno.land/x/checksum/mod.ts'\n\n// sha1\nconsole.log(new Hash('sha1').digest(encode('hello world')).hex())\n\n// md5\nconsole.log(new Hash('md5').digest(encode('hello world')).hex())\n```\n\n## Colored output\n\nIn Node.js:\n\n```js\nimport chalk from 'chalk'\n\nconsole.log(chalk.bold(chalk.bgGreen('foo')))\n```\n\nIn Deno:\n\n```js\nconsole.log('%cfoo', 'font-weight:bold;background-color:green')\n```\n","funding_links":[],"categories":["Others"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fegoist%2Fnode-vs-deno","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fegoist%2Fnode-vs-deno","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fegoist%2Fnode-vs-deno/lists"}