Skip to content

Latest commit

 

History

History
103 lines (66 loc) · 3.6 KB

bun.md

File metadata and controls

103 lines (66 loc) · 3.6 KB

Sure, here's a more detailed guide on using bun as a developer:

Installation

First, ensure you have bun installed globally or within your project. You can install it via npm or yarn:

npm install -g @bun/cli

or

yarn global add @bun/cli

Basic Commands

Running Scripts and Applications

  • bun run <script>: Run the start script or any script specified in package.json.
  • bun run start: Specifically run the start script defined in package.json.

Package Management

  • bun install <package>: Install a specific package.
  • bun install: Install all dependencies listed in package.json.

Running Scripts and Applications

  • run: Executes a file with Bun. You can specify the script to run, e.g., bun run ./my-script.ts.
  • lint: Runs a script defined in package.json for linting purposes.

Testing

  • test: Runs unit tests with Bun.

Package Management

  • install: Installs dependencies for a package.json file. Equivalent to bun i.
  • add: Adds a dependency to package.json. Syntax: bun add <package>.
  • remove: Removes a dependency from package.json. Syntax: bun remove <package>.
  • update: Updates outdated dependencies.
  • link: Registers or links a local npm package.
  • unlink: Unregisters a local npm package.
  • pm: Additional package management utilities.

Bundling and Building

  • build: Bundles TypeScript & JavaScript files into a single file. Syntax: bun build ./a.ts ./b.jsx.

Miscellaneous

  • repl: Starts a REPL (Read-Eval-Print Loop) session with Bun.
  • create: Creates a new project from a template. Syntax: bun create <template>.

Initialization and Upgrading

  • init: Starts an empty Bun project from a blank template.
  • upgrade: Upgrades to the latest version of Bun.

Additional Options

  • <command> --help: Prints help text for a specific command.

  • bunx <package> '<command>': Execute a package directly, similar to npx.

Bundling and Building

  • bun build ./index.tsx: Bundle a project for browsers.

Testing

  • bun test: Run tests using Bun's built-in test runner.

Miscellaneous

  • bun create <template> [<destination>]: Scaffold a new project using a template.
  • bun run --filter <workspace> <script>: Run multiple workspace scripts in parallel.

Flags and Options

bun provides several flags and options to customize its behavior:

  • --watch: Automatically restart the process on file change.
  • --hot: Enable auto reload in the Bun runtime, test runner, or bundler.
  • --smol: Use less memory but run garbage collection more often.
  • --inspect, --inspect-wait, --inspect-brk: Activate Bun's debugger.
  • --if-present: Exit without an error if the entrypoint does not exist.
  • --no-install, --install: Configure auto-install behavior.
  • --prefer-offline, --prefer-latest: Control package resolution behavior.
  • --conditions: Pass custom conditions to resolve.
  • --silent: Don't print the script command.
  • --env-file: Load environment variables from specified file(s).
  • --cwd: Specify the absolute path to resolve files & entry points from.
  • --config: Specify the path to Bun config file.

Additional Resources

This guide covers the basic usage of bun for common development tasks. For more advanced usage or specific use cases, refer to the official Bun documentation and community resources.