{"id":13903048,"url":"https://github.com/milliorn/bunjs-starter","last_synced_at":"2025-06-15T19:05:34.192Z","repository":{"id":195662408,"uuid":"693303722","full_name":"milliorn/bunjs-starter","owner":"milliorn","description":"This is a starter repo for Bun.js is a JavaScript runtime, bundler, toolkit. ","archived":false,"fork":false,"pushed_at":"2025-02-01T05:13:37.000Z","size":46,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-25T23:22:44.988Z","etag":null,"topics":["bundler","bunjs","cli","development","framework","interactive","macros","node","nodejs","routes","runtime","scaffold","starter","toolkit","web-framework"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/milliorn.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-18T18:56:56.000Z","updated_at":"2025-02-01T05:13:39.000Z","dependencies_parsed_at":"2023-09-19T05:13:48.564Z","dependency_job_id":"07999bb2-3eee-450b-afad-3a825c9137cc","html_url":"https://github.com/milliorn/bunjs-starter","commit_stats":null,"previous_names":["milliorn/bun-starter"],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milliorn%2Fbunjs-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milliorn%2Fbunjs-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milliorn%2Fbunjs-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milliorn%2Fbunjs-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/milliorn","download_url":"https://codeload.github.com/milliorn/bunjs-starter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514221,"owners_count":21116899,"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":["bundler","bunjs","cli","development","framework","interactive","macros","node","nodejs","routes","runtime","scaffold","starter","toolkit","web-framework"],"created_at":"2024-08-06T22:01:35.284Z","updated_at":"2025-04-12T03:42:39.538Z","avatar_url":"https://github.com/milliorn.png","language":"TypeScript","funding_links":[],"categories":["cli"],"sub_categories":[],"readme":"# bun-starter\n\n[![CodeQL](https://github.com/milliorn/bunjs-starter/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/milliorn/bunjs-starter/actions/workflows/github-code-scanning/codeql)\n\nThis is a starter repo for Bun.js is a JavaScript runtime, bundler, toolkit.   It is designed to be a simple, fast, and easy to use tool for JavaScript developers.\n\n# Table of Contents\n\n1. [Introduction](#introduction)\n2. [Bun.js Website](#bunjs-website)\n3. [Install Bun.js](#install-bunjs)\n4. [Getting Started](#getting-started)\n   - [Quickstart](#quickstart)\n   - [Run a File](#run-a-file)\n5. [Running Modes and Environment Configuration](#running-modes-and-environment-configuration)\n   - [Watch Mode](#watch-mode)\n   - [Hot Mode](#hot-mode)\n   - [Environment Variables](#environment-variables)\n6. [Working with Routes and File I/O](#working-with-routes-and-file-io)\n   - [Routes](#routes)\n   - [File I/O](#file-io)\n7. [Bun X](#bun-x)\n8. [Examples](#examples)\n9. [Testing and Bundling](#testing-and-bundling)\n   - [Bun Test](#bun-test)\n   - [Bundler](#bundler)\n10. [Macros](#macros)\n\n## Bun.js Website\n\nhttps://bun.sh/\n\n## Install Bun.js\n\n```bash\ncurl -fsSL https://bun.sh/install | bash\n```\n\nSupported on macOS, Linux, and WSL. Windows is not supported yet. See [Installation](https://bun.sh/#/docs/installation) for more details.\n\nWhen you install Bun.js, you will see something like this:\n\n```bash\nbun was installed successfully to ~/.bun/bin/bun\n\nAdded \"~/.bun/bin\" to $PATH in \"~/.bashrc\"\n\nTo get started, run:\n\n  source /home/milliorn/.bashrc\n  bun --help\n```\n\nTo test the installation run:\n\n```bash\nbun --help\n```\n\nand to confirm you can run:\n\n```bash\nbun --version\n```\n\n## Quickstart\n\nSee [Quickstart](https://bun.sh/docs/quickstart) for more details.\n\nRun `bun init` to scaffold a new project. It's an interactive tool; for this tutorial, just press enter to accept the default answer for each prompt.\n\n```bash\nbun init helps you get started with a minimal project and tries to guess sensible defaults. Press ^C anytime to quit\n\npackage name (bun-starter):\nentry point (server.ts):\n\nDone! A package.json file was saved in the current directory.\n + server.ts\n + tsconfig.json (for editor auto-complete)\n\nTo get started, run:\n  bun run server.ts\n```\n\n## Run a file\n\nSee [Run a script](https://bun.sh/docs/quickstart#run-a-script) for more details.\n\nIn your cli in the root of your project run:\n\n```bash\nbun run server.ts\n```\n\nNow, open `package.json` in your editor. You can add this to your scripts:\n\n```json\n{\n  \"scripts\": {\n    \"start\": \"bun run server.ts\"\n  }\n}\n```\n\nThen run it with bun run start.\n\n```bash\nbun run start\n```\n\nNow, open `server.ts` in your editor and paste the following code:\n\n```ts\nconsole.log(\"Hello via Bun!\");\n\nconst server = Bun.serve({\n  port: 3000,\n  fetch(req) {\n    return new Response(\"Bun!\");\n  },\n});\n\nconsole.log(`Listening on http://localhost:${server.port}`);\n```\n\nNow, open `http://localhost:3000` in your browser. You should see \"Bun!\".\n\n## Watch mode\n\nSee [Watch mode](https://bun.sh/docs/quickstart#watch-mode) for more details.\n\nIn your cli in the root of your project run:\n\n```bash\nbun run --watch server.ts\n```\n\n## Hot Mode\n\nSee [Hot Mode](https://bun.sh/docs/quickstart#hot-mode) for more details.\n\nUse `bun --hot` to enable hot reloading when executing code with Bun.\n\n```bash\nbun run --hot server.ts\n```\n\n## Environment variables\n\nSee [Environment variables](https://bun.sh/docs/quickstart#environment-variables) for more details.\n\nBun uses the dotenv package to manage environment variables. Create a `.env` file in the root of your project:\n\n```bash\ntouch .env\n```\n\nAdd the following to your `.env` file:\n\n```bash\nPORT=3000\n```\n\nNow, open `server.ts` in your editor and replace the port with the following:\n\n```ts\nport: process.env.PORT || 3001;\n```\n\nShould look like this:\n\n```ts\nconsole.log(\"Hello via Bun!\");\n\nconst server = Bun.serve({\n  port: process.env.PORT || 3001,\n  fetch(req) {\n    return new Response(\"Bun run!\");\n  },\n});\n\nconsole.log(`Listening on http://localhost:${server.port}`);\n```\n\nAlternatively, you can use the `bun` instead of `process.env`:\n\n```ts\nport: Bun.env.PORT || 3001;\n```\n\n## Routes\n\nWe can create simple routes:\n\n```ts\nconsole.log(\"Hello via Bun!\");\n\nconst server = Bun.serve({\n  port: process.env.PORT || 3001,\n  fetch(req) {\n    const url = new URL(req.url);\n\n    if (url.pathname === \"/\") {\n      return new Response(\"Hello World!\");\n    } else if (url.pathname === \"/json\") {\n      return new Response(JSON.stringify({ hello: \"world\" }), {\n        headers: { \"content-type\": \"application/json\" },\n      });\n    } else {\n      return new Response(\"Not Found\", { status: 404 });\n    }\n  },\n});\n\nconsole.log(`Listening on http://localhost:${server.port}`);\n```\n\nNow, if you go to `http://localhost:3001/json` you should see:\n\n```json\n{ \"hello\": \"world\" }\n```\n\n## Bun X\n\n`bunx \u003cpackage\u003e` is a command line tool that allows you to run a package without installing it, similar to `npx`.\n\n```bash\nbunx cowsay \"hello\"\n```\n\n```bash\n _______\n\u003c hello \u003e\n -------\n        \\   ^__^\n         \\  (oo)\\_______\n            (__)\\       )\\/\\\n                ||----w |\n                ||     ||\n```\n\n## File I/O\n\nSee [File I/O](https://bun.sh/docs/api/file-io) for more details.\n\nMake a new file called `writeFile.ts` or whatever you want it to be in the root of your project and add the following text:\n\n```ts\nconst dummyData = \"Hello World!\";\n\nawait Bun.write(\"writeFile.txt\", dummyData);\n```\n\nIf you don't use `writeFile.ts` as the name of the file, make sure to change the name in the parameter of `Bun.write()`.\n\nNow you can run the code with:\n\n```bash\nbun run writeFile.ts\n```\n\nYou should see a new file called `writeFile.txt` in the root of your project with the text \"Hello World!\".\n\nTo read the file, make a new file called `readFile.ts` or whatever you want it to be in the root of your project and add the following text:\n\n```ts\nconst readFlie = Bun.file(\"writeFile.txt\");\nconsole.log(await readFlie.text());\n```\n\n```bash\nbun-starter$ bun run readFile.ts\n[3.24ms] \".env\"\nHello World!\n```\n\n## bun test\n\nSee [bun test](https://bun.sh/docs/cli/test) for more details.\n\nBun.js comes with a built-in test runner. To run tests, create a file called `index.test.ts` or whatever you want it to be in the root of your project and add the following text:\n\n```ts\nimport { describe, expect, test, beforeAll } from \"bun:test\";\n\ndescribe(\"math\", () =\u003e {\n  test(\"logic\", () =\u003e {\n    expect(1).toBe(1);\n  });\n});\n```\n\nNow you can run the code with:\n\n```bash\nbun test\n```\n\nYou should see:\n\n```bash\n\n  math\n    ✓ logic\n\n  1 passing (1ms)\n```\n\n## Bundler\n\nSee [Bundler](https://bun.sh/docs/bundler) for more details.\n\nBun.js comes with a built-in bundler. To bundle your code, create a file called `index.ts` or whatever you want it to be in `src` folder of your project and add the following text:\n\n```ts\nimport axios from \"axios\";\n\nasync function fetchUser(user: string) {\n  try {\n    const response = await axios.get(`https://api.github.com/users/${user}`);\n    return response.data;\n  } catch (error) {\n    console.error(\"Error fetching user:\", error);\n    throw error;\n  }\n}\n\nfetchUser(\"milliorn\")\n  .then((data) =\u003e console.log(\"User data:\", data))\n  .catch((error) =\u003e console.error(\"Error:\", error));\n```\n\nNow you can bundle the code with:\n\n```bash\nbun build src/index.ts --outfile dist/bundle.js\n```\n\nYou should see a new file called `bundle.js` in the `dist` folder of your project with the bundled code.\n\n## Macros\n\nSee [Macros](https://bun.sh/docs/bundler/macros) for more details.\n\nMacros are a mechanism for running JavaScript functions at bundle-time. The value returned from these functions are directly inlined into your bundle.\n\nCreate a file called `random.ts` or whatever you want it to be in `src` folder of your project and add the following text:\n\n```ts\nexport function random() {\n  return Math.random();\n}\n```\n\nNow you can use the macro in `cli.tsx`:\n\n```ts\nimport { random } from './random.ts' with { type: 'macro' };\n\nconsole.log(`Your random number is ${random()}`);\n```\n\nNow you can bundle the code with:\n\n```bash\nbun build src/cli.tsx\n```\n\nAt the time of this writing TypeScript does not support macros as it will throw linter errors. It will work.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmilliorn%2Fbunjs-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmilliorn%2Fbunjs-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmilliorn%2Fbunjs-starter/lists"}