{"id":17794267,"url":"https://github.com/pyk/node-playground","last_synced_at":"2025-04-02T02:21:38.163Z","repository":{"id":71957649,"uuid":"129572487","full_name":"pyk/node-playground","owner":"pyk","description":null,"archived":false,"fork":false,"pushed_at":"2018-04-16T00:12:09.000Z","size":19,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-07T17:23:20.081Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/pyk.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-15T03:08:06.000Z","updated_at":"2018-04-16T06:06:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"727de41f-3c27-41be-90b2-93ca993748e8","html_url":"https://github.com/pyk/node-playground","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/pyk%2Fnode-playground","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyk%2Fnode-playground/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyk%2Fnode-playground/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyk%2Fnode-playground/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pyk","download_url":"https://codeload.github.com/pyk/node-playground/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246741317,"owners_count":20826104,"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-10-27T11:15:41.725Z","updated_at":"2025-04-02T02:21:38.140Z","avatar_url":"https://github.com/pyk.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Node.js Playground\nThis is my Node.js playground.\n\nRun the following commands to run locally:\n\n    git clone https://github.com/pyk/node-playground.git\n    cd node-playground\n    npm install\n\nThere are a plugins for VS Code that useful:\n\n* [ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint).\n  This plugin will helps you to catch a typo/error without running the code.\n\n\n## Const\nIn this section we will talk about `const` because it is commonly\nused in a Node.js codebase.\n\n### What is `const`?\n\n`const` is a keyword to represent a constant value. A constant value\nis a value that never change, if the value is change then it raise an\nerror.\n\n### Example\n\n```javascript\n// const.js\nconst number = 10;\n\nnumber = 2;\n```\n\nRun:\n\n    node const.js\n\nIt will throw an error:\n\n    TypeError: Assignment to constant variable.\n        at Object.\u003canonymous\u003e (/home/pyk/go/src/github.com/pyk/node-playground/const.js:3:8)\n        at Module._compile (internal/modules/cjs/loader.js:654:30)\n        at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)\n        at Module.load (internal/modules/cjs/loader.js:566:32)\n        at tryModuleLoad (internal/modules/cjs/loader.js:506:12)\n        at Function.Module._load (internal/modules/cjs/loader.js:498:3)\n        at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)\n        at startup (internal/bootstrap/node.js:201:19)\n\n\n## Modules\nThere are 3 different types of modules:\n\n1. **Core Modules**. The core modules are defined within Node.js's\n   source and are located in the lib/ folder. To load the core modules\n   use the following syntax `require($ID)` where `$ID` is like 'http'\n   or 'fs'.\n2. **File Modules**. The file modules is a module that created by user.\n   The module can be loaded as `reuire('filename.js')`. Only exported\n   definition are available inside the caller.\n3. **Folders as Modules**. The folder should contains file named `index.js`.\n   The module can be loaded as `require('foldername')` where `foldername/index.js`\n   is expected to exists.\n\n### Core Modules Example\n\n```javascript\nconst fs = require('fs');\nconst path = require('path');\nconst util = require('util');\n\n// Promisify callback-based function\nconst readFile = util.promisify(fs.readFile);\n\nconst main = async () =\u003e {\n  const filePath = path.join(__dirname, 'data.txt');\n  // TODO: async/await node.js 9\n  const data = await readFile(filePath, { encoding: 'utf8', flag: 'r' });\n  console.log(data);\n};\n\nmain();\n```\n\n### File Modules Example\nWe create new file called `math.js` with the following content:\n\n```javascript\nconst add = (a, b) =\u003e a + b;\nconst sub = (a, b) =\u003e a - b;\n\n// Exports the functions\nexports.add = add;\nexports.sub = sub;\n```\n\nNotice these two lines:\n\n```javascript\nexports.add = add;\nexports.sub = sub;\n```\n\nThese lines are required in order to use `add` and `sub` functions\noutside the `math.js`.\n\nThen we add new `math.js` consumer file named `main.js` with the\nfollowing content:\n\n```javascript\nconst math = require('./math.js');\n\nconsole.log(math.add(1, 2));\nconsole.log(math.sub(4, 8));\n```\n\nRun the `main.js`, you will get the following output:\n\n```\n3\n-4\n```\n\n### Folder Modules Example\nWe create new file called `some-lib/index.js` with the same content as `math.js`.\n\nUpdate the `main.js` to this:\n```javascript\n// const math = require('./math.js');\nconst math = require('./some-lib');\n\nconsole.log(math.add(1, 2));\nconsole.log(math.sub(4, 8));\n```\n\nyou should get the same output.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyk%2Fnode-playground","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpyk%2Fnode-playground","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyk%2Fnode-playground/lists"}