DEV Community

Cover image for Learn Bun.sh in Simple ways
Aakash
Aakash

Posted on

Learn Bun.sh in Simple ways

Bun.sh is a modern JavaScript runtime similar to Node.js, but it promises better performance and built-in tools for tasks such as bundling, transpiling, and running scripts.
10 Facts about Bun.sh

  1. Bun.sh is a modern JavaScript runtime designed for better performance than Node.js.
  2. It includes built-in tools for bundling, transpiling, and running scripts.
  3. Bun.sh supports hot reloading, making development faster and easier.
  4. It can directly run both JavaScript and TypeScript files without separate compilation steps.
  5. Bun.sh uses the npm ecosystem for managing dependencies.
  6. The installation of Bun.sh can be done via a simple curl command or through npm.
  7. Bun.sh aims to simplify the development workflow with integrated tools and faster execution.
  8. It is designed to work seamlessly with existing JavaScript and TypeScript projects.
  9. Bun.sh provides a command-line interface for initializing new projects and managing builds.
  10. Its built-in bundler can create production-ready builds from your source code.

bun.sh

1. Install Bun.sh

To install Bun.sh, you'll need to run the following script from your terminal:

for powershell(sh)

curl -fsSL https://bun.sh/install | bash

Enter fullscreen mode Exit fullscreen mode

Basic commands in bun.sh

  1. ## Initialize a new project

For init a new project in powershell(sh)

bun init

Enter fullscreen mode Exit fullscreen mode

This sets up a new project directory with the necessary files.

2. Run a JavaScript or TypeScript file:

bun run <file>

Enter fullscreen mode Exit fullscreen mode

For example, to run index.js

//eg file name index.js
bun run index.js
Enter fullscreen mode Exit fullscreen mode

3. Install dependencies:

bun install <package-name>
Enter fullscreen mode Exit fullscreen mode

Eg: To install express

bun install express

Enter fullscreen mode Exit fullscreen mode

4. Add Dependency to your Project


bun add <package-name>

Enter fullscreen mode Exit fullscreen mode

This will add the package to your package.json and install it.

5. Remove a dependency from your project:

bun remove <package-name>
Enter fullscreen mode Exit fullscreen mode

6. Bundle your code:

bun bundle <file>
Enter fullscreen mode Exit fullscreen mode

For example, to bundle index.js:

bun bundle index.js
Enter fullscreen mode Exit fullscreen mode

7.Run the project with hot reloading:

bun run --hot <file>
Enter fullscreen mode Exit fullscreen mode

For example, to run server.js with hot reloading:

bun run --hot server.js
Enter fullscreen mode Exit fullscreen mode

8.Check the Bun.sh version:

bun --version
Enter fullscreen mode Exit fullscreen mode

9.Build your project for production:

bun build

Enter fullscreen mode Exit fullscreen mode

This will create a production build of your project.

10. Update Bun.sh to the latest version:

bun upgrade
Enter fullscreen mode Exit fullscreen mode

These commands cover the basic functionalities you will use when working with Bun.sh, from initializing projects and running files to managing dependencies and building for production.

Top comments (0)