{"id":15662400,"url":"https://github.com/justjavac/node2deno","last_synced_at":"2025-05-05T23:27:47.587Z","repository":{"id":51005217,"uuid":"369388758","full_name":"justjavac/node2deno","owner":"justjavac","description":"Deno for Node.js developers","archived":false,"fork":false,"pushed_at":"2021-05-26T08:30:28.000Z","size":42,"stargazers_count":18,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-07T18:03:54.161Z","etag":null,"topics":["deno","nodejs"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/justjavac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-21T02:10:57.000Z","updated_at":"2024-03-10T01:17:49.000Z","dependencies_parsed_at":"2022-09-25T01:51:29.170Z","dependency_job_id":null,"html_url":"https://github.com/justjavac/node2deno","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/justjavac%2Fnode2deno","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justjavac%2Fnode2deno/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justjavac%2Fnode2deno/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/justjavac%2Fnode2deno/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/justjavac","download_url":"https://codeload.github.com/justjavac/node2deno/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252592257,"owners_count":21773230,"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":["deno","nodejs"],"created_at":"2024-10-03T13:32:18.872Z","updated_at":"2025-05-05T23:27:47.568Z","avatar_url":"https://github.com/justjavac.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# From Node.js to Deno\n\nDeno for Node.js developers.\n\n## Console\n\n### Class `Console`\n\n```ts\n// Node\nconst { Console } = console;\nnew Console({ stdout: process.stdout, stderr: process.stderror });\n\n// Deno\nconst { Console } = console;\n// undefined\n\n// Chrome\nconst { Console } = console;\n// undefined\n```\n\n### `console.error`\n\n```ts\n// Node\nconsole.error(new Error(\"Whoops, something bad happened\"));\n// Error: Whoops, something bad happened\n//     at REPL1:1:15\n//     at Script.runInThisContext (vm.js:133:18)\n//     at REPLServer.defaultEval (repl.js:484:29)\n//     at bound (domain.js:413:15)\n//     at REPLServer.runBound [as eval] (domain.js:424:12)\n//     at REPLServer.onLine (repl.js:817:10)\n//     at REPLServer.emit (events.js:327:22)\n//     at REPLServer.EventEmitter.emit (domain.js:467:12)\n//     at REPLServer.Interface._onLine (readline.js:337:10)\n//     at REPLServer.Interface._line (readline.js:666:8)\n\n// Deno\nconsole.error(new Error(\"Whoops, something bad happened\"));\n// Error: Whoops, something bad happened\n//     at \u003canonymous\u003e:2:15\n\n// Chrome\nconsole.error(new Error(\"Whoops, something bad happened\"));\n// Error: Whoops, something bad happened\n//     at \u003canonymous\u003e:1:15\n```\n\n### `console.trace`\n\n```ts\n// Node\nconsole.trace(\"Show me\");\n// Trace: Show me\n//     at REPL14:1:9\n//     at Script.runInThisContext (vm.js:133:18)\n//     at REPLServer.defaultEval (repl.js:484:29)\n//     at bound (domain.js:413:15)\n//     at REPLServer.runBound [as eval] (domain.js:424:12)\n//     at REPLServer.onLine (repl.js:817:10)\n//     at REPLServer.emit (events.js:327:22)\n//     at REPLServer.EventEmitter.emit (domain.js:467:12)\n//     at REPLServer.Interface._onLine (readline.js:337:10)\n//     at REPLServer.Interface._line (readline.js:666:8)\n\n// Deno\nconsole.trace(\"Show me\");\n// Trace: Show me\n//     at \u003canonymous\u003e:2:9\n\n// Chrome\nconsole.trace(\"Show me\");\n// Show me\n// (anonymous) @ VM45:1\n```\n\n## File system\n\n### Reading file\n\n```ts\n// Node\nimport fs from \"fs/promises\";\nconst data = await fs.readFile(\"./README.md\", \"utf8\");\n\n// Deno\n// Requires `--allow-read` permission\nconst data = await Deno.readTextFile(\"./README.md\");\n```\n\n### Writing file\n\n```ts\n// Node\nimport fs from \"fs/promises\";\nconst content = \"Some content!\";\nawait fs.writeFile(\"./file.txt\", content);\n\n// Deno\n// Requires `--allow-write` permission\nconst content = \"Some content!\";\nawait Deno.writeTextFile(\"./file.txt\", content);\n```\n\n### File descriptors\n\n```ts\n// Node\nimport fs from \"fs/promises\";\nconst file = await fs.open(\"./file.txt\");\n\n// Deno\n// Requires `allow-read` and/or `allow-write` permissions depending on options.\nconst file = await Deno.open(\"./file.txt\");\n```\n\n### Copying file\n\n```ts\n// Node\nimport fs from \"fs/promises\";\nawait fs.copyFile('source.txt', 'target.txt');\n\n// Deno\n// Requires `allow-read` and/or `allow-write` permissions depending on options.\nimport { copy } from \"https://deno.land/std@0.97.0/fs/mod.ts\";\nawait copy('./source.txt', './target.txt');\n```\n\n### Moveing file\n\n```ts\n// Node\nimport fs from \"fs/promises\";\nawait fs.rename('source.txt', 'target.txt');\n\n// Deno\n// Requires `allow-read` and/or `allow-write` permissions depending on options.\nimport { move } from \"https://deno.land/std@0.97.0/fs/mod.ts\";\nawait move('./source.txt', './target.txt');\n```\n\n## Path\n\n### Last portion of a path\n\n```ts\n// Node\nimport path from \"path\";\npath.basename(\"/foo/bar/baz/asdf/quux.html\");\n// -\u003e 'quux.html'\n\n// Deno\nimport * as path from \"https://deno.land/std/path/mod.ts\";\npath.basename(\"/foo/bar/baz/asdf/quux.html\");\n// -\u003e 'quux.html'\n```\n\nOr with file extensions:\n\n```ts\n// Node\nimport path from \"path\";\npath.basename(\"/foo/bar/baz/asdf/quux.html\", \".html\");\n// -\u003e 'quux'\n\n// Deno\nimport * as path from \"https://deno.land/std/path/mod.ts\";\npath.basename(\"/foo/bar/baz/asdf/quux.html\", \".html\");\n// -\u003e 'quux'\n```\n\n### Directory name of a path\n\n```ts\n// Node\nimport path from \"path\";\npath.dirname(\"/foo/bar/baz/asdf/quux\");\n// -\u003e '/foo/bar/baz/asdf'\n\n// Deno\nimport * as path from \"https://deno.land/std/path/mod.ts\";\npath.dirname(\"/foo/bar/baz/asdf/quux\");\n// -\u003e '/foo/bar/baz/asdf'\n```\n\n### Extension of the path\n\n```ts\n// Node\nimport path from \"path\";\npath.extname(\"index.html\");\n// -\u003e '.html'\n\n// Deno\nimport * as path from \"https://deno.land/std/path/mod.ts\";\npath.extname(\"index.html\");\n// -\u003e '.html'\n```\n\n### Join all path segments together\n\n```ts\n// Node\nimport path from \"path\";\npath.join(\"/foo\", \"bar\", \"baz/asdf\", \"quux\", \"..\");\n// -\u003e '/foo/bar/baz/asdf'\n\n// Deno\nimport * as path from \"https://deno.land/std/path/mod.ts\";\npath.join(\"/foo\", \"bar\", \"baz/asdf\", \"quux\", \"..\");\n// -\u003e '/foo/bar/baz/asdf'\n```\n\n### Resolves path segments into an absolute path.\n\n```ts\n// Node\nimport path from \"path\";\npath.resolve('/foo/bar', './baz');\n// -\u003e '/foo/bar/baz'\n\n// Deno\nimport * as path from \"https://deno.land/std/path/mod.ts\";\npath.resolve('/foo/bar', './baz');\n// -\u003e '/foo/bar/baz'\n```\n\n### Normalizes a path\n\n```ts\n// Node\nimport path from \"path\";\npath.normalize(\"/foo/bar//baz/asdf/quux/..\");\n// -\u003e '/foo/bar/baz/asdf'\n\n// Deno\nimport * as path from \"https://deno.land/std/path/mod.ts\";\npath.normalize(\"/foo/bar//baz/asdf/quux/..\");\n// -\u003e '/foo/bar/baz/asdf'\n```\n\n### Parse a path\n\n```ts\n// Node\nimport path from \"path\";\npath.parse(\"/home/user/dir/file.txt\");\n// -\u003e { root: \"/\", dir: \"/home/user/dir\", base: \"file.txt\", ext: \".txt\", name: \"file\" }\n\n// Deno\nimport * as path from \"https://deno.land/std/path/mod.ts\";\npath.parse(\"/home/user/dir/file.txt\");\n// -\u003e { root: \"/\", dir: \"/home/user/dir\", base: \"file.txt\", ext: \".txt\", name: \"file\" }\n```\n\n## Subprocess\n\nTODO: needs improvement\n\n### Subprocess in a short story\n\n```ts\n// Node\n// All functionalities associated to subprocess are under `child_process` module\nconst child_process = require(\"child_process\");\n// Spawn a subprocess\nconst p = child_process.spawn(\"ping\", [\"-h\"]);\n// OR buffered version `child_process.exec`\n// OR synced version `child_process.spawnSync`\n// redirect stdout\np.stdout.on(\"data\", (data) =\u003e console.log(data));\n// redirect stderr\np.stderr.on(\"data\", (error) =\u003e console.log(error));\n// Subprocess Close event\np.on(\"close\", (code) =\u003e console.log(`child process exited with code ${code}`));\n\n// Here's how we can spawn a *BRAND NEW* nodejs instance to run a js \"module\"\nchild_process.fork(\"hello.js\");\n\n// Final output:\n//  \u003cBuffer d1 a1 cf ee 20 2d 68 20 b2 bb d5 fd c8 b7 a1 a3 0d 0a 0d 0a d3 c3 b7 a8 3a 20 70 69 6e 67 20 5b 2d 74 5d 20 5b 2d 61 5d 20 5b 2d 6e 20 63 6f 75 6e 74 ... 1447 more bytes\u003e\n//  child process exited with code 1\n//  hello from hello.js\n\n// Deno\n// Deno uses `Deno.run` to spawn a subprocess\n// `allow-run` permission *REQUIRED*\nconst p = Deno.run({\n  cmd: [\"ping\", \"-h\"],\n  stdout: \"piped\",\n  stderr: \"piped\",\n});\n\n// HERE, we read out the stdout output that we set to piped before\nconsole.log(await p.output());\n// and of course the stderr\nconsole.log(await p.stderrOutput());\n\n// In Deno, we don't have a fork like function,\n// instead you can still use `Deno.run` to spawn\n// a `BRAND NEW` Deno instance to run the script\nconst decoder = new TextDecoder();\nconst forkLike = Deno.run({\n  cmd: [\"deno\", \"run\", \"hello.js\"],\n  stdout: \"piped\",\n});\nconsole.log(decoder.decode(await forkLike.output())); // hello from hello.js\n// OR in a IMO better way: `Web workers`\nnew Worker(\n  new URL(\"./hello.js\", import.meta.url).href,\n  { type: \"module\" },\n); // hello from hello.js\n\n// Final output:\n//   Uint8Array(1497) [\n//    209, 161, 207, 238,  32,  45, 104,  32, 178, 187, 213, 253, 200, 183, 161,\n//    163,  13,  10,  13,  10, 211, 195, 183, 168,  58,  32, 112, 105, 110, 103,\n//     32,  91,  45, 116,  93,  32,  91,  45,  97,  93,  32,  91,  45, 110,  32,\n//     99, 111, 117, 110, 116,  93,  32,  91,  45, 108,  32, 115, 105, 122, 101,\n//     93,  32,  91,  45, 102,  93,  32,  91,  45, 105,  32,  84,  84,  76,  93,\n//     32,  91,  45, 118,  32,  84,  79,  83,  93,  13,  10,  32,  32,  32,  32,\n//     32,  32,  32,  32,  32,  32,  32,  32,  91,  45,\n//    ... 1397 more items\n//  ]\n//  Uint8Array(0) []\n//  hello from hello.js\n\n//  hello from hello.js\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustjavac%2Fnode2deno","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjustjavac%2Fnode2deno","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustjavac%2Fnode2deno/lists"}