Professional Documents
Culture Documents
Bun Quickstart
Bun Quickstart
Bun Quickstart
First, create
a fresh directory.
```bash
$ mkdir quickstart
$ cd quickstart
```
Run `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.
```bash
$ bun init
bun init helps you get started with a minimal project and tries to
guess sensible defaults. Press ^C anytime to quit.
Since our entry point is a `*.ts` file, Bun generates a `tsconfig.json` for you. If
you're using plain JavaScript, it will generate a
[`jsconfig.json`](https://code.visualstudio.com/docs/languages/jsconfig) instead.
## Run a file
Open `index.ts` and paste the following code snippet, which implements a simple
HTTP server with [`Bun.serve`](/docs/api/http).
```ts
const server = Bun.serve({
port: 3000,
fetch(req) {
return new Response("Bun!");
},
});
```sh
$ bun add -d bun-types
```
Then add the following line to your `compilerOptions` in `tsconfig.json`.
```json-diff#tsconfig.json
{
"compilerOptions": {
+ "types": ["bun-types"]
}
}
```
{% /details %}
```bash
$ bun index.ts
Listening on http://localhost:3000 ...
```
## Run a script
Bun can also execute `"scripts"` from your `package.json`. Add the following
script:
```json-diff
{
"name": "quickstart",
"module": "index.ts",
"type": "module",
+ "scripts": {
+ "start": "bun run index.ts"
+ },
"devDependencies": {
"bun-types": "^0.7.0"
}
}
```
```bash
$ bun run start
$ bun run index.ts
Listening on http://localhost:3000 ...
```
{% callout %}
**Performance** — `bun run` is roughly 28x faster than `npm run` (6ms vs 170ms of
overhead).
{% /callout %}
## Install a package
Let's make our server a little more interesting by installing a package. First
install the `figlet` package and its type declarations. Figlet is a utility for
converting strings into ASCII art.
```bash
$ bun add figlet
$ bun add -d @types/figlet # TypeScript users only
```
```ts-diff
+ import figlet from "figlet";
Restart the server and refresh the page. You should see a new ASCII art banner.
```txt
____ _
| __ ) _ _ _ __ | |
| _ \| | | | '_ \| |
| |_) | |_| | | | |_|
|____/ \__,_|_| |_(_)
```