CLAUDE.md 2.4 K raw
1
---
2
3
Default to using Bun instead of Node.js.
4
5
- Use `bun <file>` instead of `node <file>` or `ts-node <file>`
6
- Use `bun test` instead of `jest` or `vitest`
7
- Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
8
- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
9
- Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
10
- Bun automatically loads .env, so don't use dotenv.
11
12
## APIs
13
14
- `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
15
- `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
16
- `Bun.redis` for Redis. Don't use `ioredis`.
17
- `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
18
- `WebSocket` is built-in. Don't use `ws`.
19
- Prefer `Bun.file` over `node:fs`'s readFile/writeFile
20
- Bun.$`ls` instead of execa.
21
22
## Testing
23
24
Use `bun test` to run tests.
25
26
```ts#index.test.ts
27
import { test, expect } from "bun:test";
28
29
test("hello world", () => {
30
  expect(1).toBe(1);
31
});
32
```
33
34
## Frontend
35
36
Use HTML imports with `Bun.serve()`. Don't use `vite`. HTML imports fully support React, CSS, Tailwind.
37
38
Server:
39
40
```ts#index.ts
41
import index from "./index.html"
42
43
Bun.serve({
44
  routes: {
45
    "/": index,
46
    "/api/users/:id": {
47
      GET: (req) => {
48
        return new Response(JSON.stringify({ id: req.params.id }));
49
      },
50
    },
51
  },
52
  // optional websocket support
53
  websocket: {
54
    open: (ws) => {
55
      ws.send("Hello, world!");
56
    },
57
    message: (ws, message) => {
58
      ws.send(message);
59
    },
60
    close: (ws) => {
61
      // handle close
62
    }
63
  },
64
  development: {
65
    hmr: true,
66
    console: true,
67
  }
68
})
69
```
70
71
HTML files can import .tsx, .jsx or .js files directly and Bun's bundler will transpile & bundle automatically. `<link>` tags can point to stylesheets and Bun's CSS bundler will bundle.
72
73
```html#index.html
74
<html>
75
  <body>
76
    <h1>Hello, world!</h1>
77
    <script type="module" src="./frontend.tsx"></script>
78
  </body>
79
</html>
80
```
81
82
With the following `frontend.tsx`:
83
84
```tsx#frontend.tsx
85
import React from "react";
86
87
// import .css files directly and it works
88
import './index.css';
89
90
import { createRoot } from "react-dom/client";
91
92
const root = createRoot(document.body);
93
94
export default function Frontend() {
95
  return <h1>Hello, world!</h1>;
96
}
97
98
root.render(<Frontend />);
99
```
100
101
Then, run index.ts
102
103
```sh
104
bun --hot ./index.ts
105
```
106
107
For more information, read the Bun API docs in `node_modules/bun-types/docs/**.md`.