温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/docs/quickstart
点击访问原文链接

Quickstart - Bun

Quickstart - BunDocumentation Index

Fetch the complete documentation index at: /docs/llms.txt

Use this file to discover all available pages before exploring further.

Skip to main contentBun home pageSearch...⌘KInstall BunSearch...NavigationGet StartedQuickstartRuntimePackage ManagerBundlerTest RunnerGuidesReferenceBlogFeedback:first-child]:!hidden peer-[.is-custom]:[&>:first-child]:sm:!hidden peer-[.is-custom]:[&>:first-child]:md:!hidden peer-[.is-custom]:[&>:first-child]:lg:!hidden peer-[.is-custom]:[&>:first-child]:xl:!hidden">Get StartedWelcome to BunInstallationQuickstartTypeScriptTypeScript 6 and 7bun initbun createCore RuntimeBun RuntimeWatch ModeDebuggingREPLbunfig.tomlFile & Module SystemFile TypesModule ResolutionJSXAuto-installPluginsFile System RouterHTTP serverServerRoutingCookiesTLSError HandlingMetricsNetworkingFetchWebSocketsTCPUDPDNSData & StorageCookiesFile I/OStreamsBinary DataArchiveSQLSQLiteS3RedisConcurrencyWorkersProcess & SystemEnvironment VariablesShellSpawnWebViewCronInterop & ToolingNode-APIFFIC CompilerTranspilerUtilitiesCSRF ProtectionSecretsConsoleTOMLYAMLMarkdownJSON5JSONLHTMLRewriterImageHashingGlobSemverColorUtilsStandards & CompatibilityGlobalsBun APIsWeb APIsNode.js CompatibilityContributingRoadmapBenchmarkingContributingBuilding WindowsBindgenLicenseOn this pageOverviewRun a scriptGet StartedQuickstartCopy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_n4ctdbsnlht5lebsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">*]:[overflow-wrap:anywhere]">

Build your first app with Bun

Copy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_1cctdbsnlht5lebsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">​Overview Build a minimal HTTP server with Bun.serve, run it locally, then evolve it by installing a package. Prerequisites: Bun installed and available on your PATH. See installation for setup. 1

Step 1

Initialize a new project with bun init.terminal
bun init my-app
It’ll prompt you to pick a template, either Blank, React, or Library. For this guide, we’ll pick Blank.terminal
bun init my-app
✓ Select a project template: Blank

- .gitignore
- CLAUDE.md
- .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc -> CLAUDE.md
- index.ts
- tsconfig.json (for editor autocomplete)
- README.md
This automatically creates a my-app directory with a basic Bun app.2

Step 2

Run the index.ts file using bun run index.ts.terminal
cd my-app
bun run index.ts
Hello via Bun!
You should see a console output saying "Hello via Bun!".3

Step 3

Replace the contents of index.ts with the following code:index.ts
const server = Bun.serve({
port: 3000,
routes: {
"/": () => new Response('Bun!'),
}
});

console.log(`Listening on ${server.url}`);
Run the index.ts file again using bun run index.ts.terminal
bun run index.ts
Listening on http://localhost:3000
Visit http://localhost:3000 to test the server. You should see a page that says "Bun!".

Seeing TypeScript errors on Bun?

If you used bun init, Bun will have automatically installed Bun’s TypeScript declarations and configured your tsconfig.json. If you’re trying out Bun in an existing project, you may see a type error on the Bun global.To fix this, first install @types/bun as a dev dependency.terminal
bun add -d @types/bun
Then add the following to your compilerOptions in tsconfig.json:tsconfig.json
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "Preserve",
"moduleDetection": "force",
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true
}
}
4

Step 4

Install the figlet package and its type declarations. Figlet is a utility for converting strings into ASCII art.terminal
bun add figlet
bun add -d @types/figlet # TypeScript users only
Update index.ts to use figlet in routes.index.ts
import figlet from 'figlet';

const server = Bun.serve({
port: 3000,
routes: {
"/": () => new Response('Bun!'),
"/figlet": () => {
const body = figlet.textSync('Bun!');
return new Response(body);
}
}
});

console.log(`Listening on ${server.url}`);
Run the index.ts file again using bun run index.ts.terminal
bun run index.ts
Listening on http://localhost:3000
Visit http://localhost:3000/figlet to test the server. You should see a page that says "Bun!" in ASCII art.
____ _
| __ ) _ _ _ __ | |
| _ \| | | | '_ \| |
| |_) | |_| | | | |_|
|____/ \__,_|_| |_(_)
5

Step 5

Let’s add some HTML. Create a new file called index.html and add the following code:index.html
DOCTYPE html>
html lang="en">
head>
meta charset="UTF-8">
meta name="viewport" content="width=device-width, initial-scale=1.0">
title>Buntitle>
head>
body>
h1>Bun!h1>
body>
html>
Then, import this file in index.ts and serve it from the root / route.index.ts
import figlet from 'figlet';
import index from './index.html';

const server = Bun.serve({
port: 3000,
routes: {
"/": index,
"/figlet": () => {
const body = figlet.textSync('Bun!');
return new Response(body);
}
}
});

console.log(`Listening on ${server.url}`);
Run the index.ts file again using bun run index.ts.terminal
bun run index.ts
Listening on http://localhost:3000
Visit http://localhost:3000 to test the server. You should see the static HTML page. 🎉 Congratulations! You’ve built an HTTP server with Bun and installed a package. ​Run a script Bun can also execute "scripts" from your package.json. Add the following script: package.json
{
"name": "quickstart",
"module": "index.ts",
"type": "module",
"private": true,
"scripts": {
"start": "bun run index.ts"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5"
}
}
Then run it with bun run start. terminal
bun run start
Listening on http://localhost:3000
⚡️ Performance — bun run is roughly 28x faster than npm run (6ms vs 170ms of overhead).

Was this page helpful?

YesNoSuggest editsRaise issueInstallationPreviousTypeScriptNext⌘IxgithubdiscordyoutubePowered byThis documentation is built and hosted on Mintlify, a developer documentation platform

Quickstart - Bun,AI智能索引,全网链接索引,智能导航,网页索引

    Build your first app with Bun