{"id":229,"date":"2026-08-07T10:06:58","date_gmt":"2026-08-07T02:06:58","guid":{"rendered":"https:\/\/yykcj.com\/?p=229"},"modified":"2026-08-07T10:06:58","modified_gmt":"2026-08-07T02:06:58","slug":"bun-%e8%bf%90%e8%a1%8c%e6%97%b6%e5%85%a5%e9%97%a8%e4%b8%8e%e7%94%9f%e6%80%81%e6%a6%82%e8%a7%88%ef%bc%9a%e4%b8%8b%e4%b8%80%e4%bb%a3-javascript-%e5%b7%a5%e5%85%b7%e9%93%be","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=229","title":{"rendered":"Bun \u8fd0\u884c\u65f6\u5165\u95e8\u4e0e\u751f\u6001\u6982\u89c8\uff1a\u4e0b\u4e00\u4ee3 JavaScript \u5de5\u5177\u94fe"},"content":{"rendered":"<h2>\u5f00\u7bc7\uff1a\u4e3a\u4ec0\u4e48 Bun \u503c\u5f97\u4f60\u5173\u6ce8\uff1f<\/h2>\n<p>\u5728 JavaScript \u751f\u6001\u4e2d\uff0cNode.js \u957f\u671f\u5360\u636e\u4e3b\u5bfc\u5730\u4f4d\uff0c\u4f46\u5b83\u7684\u542f\u52a8\u901f\u5ea6\u3001\u5305\u7ba1\u7406\u6548\u7387\u4e00\u76f4\u4e3a\u4eba\u8bdf\u75c5\u3002Bun \u7684\u6a2a\u7a7a\u51fa\u4e16\uff0c\u8bd5\u56fe\u7528\u4e00\u5957\u5de5\u5177\u94fe\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002\u5b83\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u8fd0\u884c\u65f6\uff0c\u66f4\u662f\u4e00\u4e2a\u5305\u542b\u6253\u5305\u5668\u3001\u6d4b\u8bd5\u8fd0\u884c\u5668\u3001\u5305\u7ba1\u7406\u5668\u7684\u5168\u5bb6\u6876\u3002\u6839\u636e\u5b98\u65b9\u57fa\u51c6\uff0cBun \u7684\u542f\u52a8\u901f\u5ea6\u6bd4 Node.js \u5feb 4 \u500d\uff0c\u5b89\u88c5\u4f9d\u8d56\u6bd4 npm\/yarn \u5feb 20-100 \u500d\u3002\u8fd9\u4e9b\u6570\u5b57\u662f\u5426\u771f\u5b9e\uff1f\u672c\u6587\u5c06\u5e26\u4f60\u4eb2\u624b\u9a8c\u8bc1\uff0c\u5e76\u8bc4\u4f30 Bun \u7684\u751f\u6001\u73b0\u72b6\u3002<\/p>\n<h2>\u73af\u5883\u51c6\u5907\uff1a\u5b89\u88c5 Bun<\/h2>\n<p>Bun \u7684\u5b89\u88c5\u975e\u5e38\u7b80\u5355\uff0c\u652f\u6301 macOS\u3001Linux \u548c Windows\uff08WSL\uff09\u3002\u5728\u7ec8\u7aef\u6267\u884c\uff1a<\/p>\n<pre><code class=\"language-bash\">\ncurl -fsSL https:\/\/bun.sh\/install | bash\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9a8c\u8bc1\u7248\u672c\uff1a<\/p>\n<pre><code class=\"language-bash\">\nbun --version\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u4f7f\u7528 macOS \u4e14\u5b89\u88c5\u4e86 Homebrew\uff0c\u4e5f\u53ef\u4ee5\uff1a<\/p>\n<pre><code class=\"language-bash\">\nbrew install oven-sh\/bun\/bun\n<\/code><\/pre>\n<h2>\u6838\u5fc3\u529f\u80fd\u4e00\uff1a\u6781\u901f\u7684\u5305\u7ba1\u7406\u5668<\/h2>\n<p>Bun \u5185\u7f6e\u4e86\u5305\u7ba1\u7406\u5668\uff0c\u547d\u4ee4\u4e3a <code>bun install<\/code>\u3002\u5b83\u7684\u901f\u5ea6\u5f97\u76ca\u4e8e\u5168\u5c40\u6a21\u5757\u7f13\u5b58\u548c\u786c\u94fe\u63a5\uff0c\u907f\u514d\u4e86 npm \u7684\u91cd\u590d\u4e0b\u8f7d\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\u5e76\u5b89\u88c5\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-bash\">\nmkdir bun-demo\ncd bun-demo\nbun init -y\nbun add express\n<\/code><\/pre>\n<p><code>bun add<\/code> \u4f1a\u540c\u65f6\u66f4\u65b0 <code>package.json<\/code> \u5e76\u751f\u6210 <code>bun.lockb<\/code> \u9501\u6587\u4ef6\u3002\u6ce8\u610f\uff0c\u9501\u6587\u4ef6\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f\uff0c\u7528\u4e8e\u786e\u4fdd\u4f9d\u8d56\u7684\u786e\u5b9a\u6027\u3002<\/p>\n<p><strong>\ud83d\udca1 \u5c0f\u6280\u5de7<\/strong>\uff1a\u5982\u679c\u9879\u76ee\u5df2\u6709 <code>package-lock.json<\/code> \u6216 <code>yarn.lock<\/code>\uff0cBun \u4e5f\u80fd\u8bfb\u53d6\u5b83\u4eec\uff0c\u4f46\u5efa\u8bae\u5220\u9664\u540e\u91cd\u65b0\u751f\u6210 <code>bun.lockb<\/code> \u4ee5\u83b7\u5f97\u6700\u4f73\u6027\u80fd\u3002<\/p>\n<h2>\u6838\u5fc3\u529f\u80fd\u4e8c\uff1a\u5185\u7f6e\u6253\u5305\u5668<\/h2>\n<p>Bun \u53ef\u4ee5\u76f4\u63a5\u6253\u5305 JavaScript\/TypeScript \u6587\u4ef6\uff0c\u65e0\u9700\u989d\u5916\u914d\u7f6e\u3002\u8fd9\u5bf9\u4e8e\u5feb\u901f\u539f\u578b\u6216\u5c0f\u9879\u76ee\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u5165\u53e3\u6587\u4ef6 <code>src\/index.ts<\/code>\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n\/\/ src\/index.ts\nimport { add } from &#x27;.\/math&#x27;;\n\nconsole.log(&#x27;2 + 3 =&#x27;, add(2, 3));\n<\/code><\/pre>\n<p>\u521b\u5efa <code>src\/math.ts<\/code>\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n\/\/ src\/math.ts\nexport function add(a: number, b: number) {\n    return a + b;\n}\n<\/code><\/pre>\n<p>\u6267\u884c\u6253\u5305\uff1a<\/p>\n<pre><code class=\"language-bash\">\nbun build .\/src\/index.ts --outdir .\/dist\n<\/code><\/pre>\n<p>\u6253\u5305\u4ea7\u7269\u5728 <code>dist\/index.js<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\uff1a<\/p>\n<pre><code class=\"language-bash\">\nnode dist\/index.js\n<\/code><\/pre>\n<p>Bun \u7684\u6253\u5305\u5668\u652f\u6301 JSX\u3001TypeScript\u3001CSS \u6a21\u5757\u7b49\uff0c\u5e76\u4e14\u53ef\u4ee5 tree-shaking\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u4f60\u53ef\u80fd\u8fd8\u662f\u9700\u8981\u914d\u7f6e bundler\uff0c\u4f46 Bun \u5df2\u7ecf\u8db3\u591f\u7b80\u5355\u3002<\/p>\n<h2>\u6838\u5fc3\u529f\u80fd\u4e09\uff1a\u5185\u7f6e\u6d4b\u8bd5\u8fd0\u884c\u5668<\/h2>\n<p>Bun \u7684\u6d4b\u8bd5\u8fd0\u884c\u5668\u517c\u5bb9 Jest \u7684 API\uff0c\u4f46\u901f\u5ea6\u66f4\u5feb\u3002\u6211\u4eec\u5199\u4e00\u4e2a\u6d4b\u8bd5\u6587\u4ef6 <code>src\/math.test.ts<\/code>\uff1a<\/p>\n<pre><code class=\"language-typescript\">\nimport { describe, expect, test } from &#x27;bun:test&#x27;;\nimport { add } from &#x27;.\/math&#x27;;\n\ndescribe(&#x27;math&#x27;, () =&gt; {\n    test(&#x27;adds 2 + 3&#x27;, () =&gt; {\n        expect(add(2, 3)).toBe(5);\n    });\n});\n<\/code><\/pre>\n<p>\u8fd0\u884c\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"language-bash\">\nbun test\n<\/code><\/pre>\n<p>\u8f93\u51fa\u4f1a\u663e\u793a\u6d4b\u8bd5\u7ed3\u679c\u548c\u8017\u65f6\u3002Bun \u7684\u6d4b\u8bd5\u8fd0\u884c\u5668\u8fd8\u652f\u6301\u5feb\u7167\u6d4b\u8bd5\u3001mock \u7b49\uff0c\u4f46\u9700\u8981\u6ce8\u610f\uff0c\u67d0\u4e9b Jest \u7684\u751f\u6001\u5e93\u53ef\u80fd\u4e0d\u517c\u5bb9\u3002<\/p>\n<h2>\u6838\u5fc3\u529f\u80fd\u56db\uff1a\u4f5c\u4e3a\u8fd0\u884c\u65f6\u8fd0\u884c TypeScript<\/h2>\n<p>Bun \u539f\u751f\u652f\u6301 TypeScript\uff0c\u65e0\u9700\u7f16\u8bd1\u6b65\u9aa4\u3002\u4f60\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c <code>.ts<\/code> \u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-bash\">\nbun run src\/index.ts\n<\/code><\/pre>\n<p>\u8fd9\u5728\u5f00\u53d1\u65f6\u975e\u5e38\u65b9\u4fbf\uff0c\u56e0\u4e3a\u4f60\u4e0d\u518d\u9700\u8981 <code>ts-node<\/code> \u6216 <code>tsx<\/code>\u3002Bun \u7684\u8f6c\u8bd1\u901f\u5ea6\u6781\u5feb\uff0c\u542f\u52a8\u65f6\u95f4\u51e0\u4e4e\u53ef\u4ee5\u5ffd\u7565\u3002<\/p>\n<p><strong>\u6ce8\u610f<\/strong>\uff1aBun \u7684 TypeScript \u652f\u6301\u662f\u8f6c\u8bd1\uff08transpile\uff09\uff0c\u4e0d\u505a\u7c7b\u578b\u68c0\u67e5\u3002\u5982\u679c\u4f60\u9700\u8981\u7c7b\u578b\u68c0\u67e5\uff0c\u8bf7\u4f7f\u7528 <code>tsc --noEmit<\/code> \u6216 IDE \u63d2\u4ef6\u3002<\/p>\n<h2>\u5b9e\u6218\uff1a\u7528 Bun \u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 HTTP \u670d\u52a1\u5668<\/h2>\n<p>\u7ed3\u5408 Bun \u7684 API\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 HTTP \u670d\u52a1\u5668\u3002Bun \u63d0\u4f9b\u4e86 <code>Bun.serve<\/code> \u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-typescript\">\n\/\/ server.ts\nconst server = Bun.serve({\n    port: 3000,\n    fetch(request) {\n        const url = new URL(request.url);\n        if (url.pathname === &#x27;\/&#x27;) {\n            return new Response(&#x27;Hello from Bun!&#x27;);\n        }\n        if (url.pathname === &#x27;\/json&#x27;) {\n            return Response.json({ message: &#x27;Hello JSON&#x27; });\n        }\n        return new Response(&#x27;Not Found&#x27;, { status: 404 });\n    },\n});\n\nconsole.log(`Server running at http:\/\/localhost:${server.port}`);\n<\/code><\/pre>\n<p>\u8fd0\u884c\uff1a<\/p>\n<pre><code class=\"language-bash\">\nbun run server.ts\n<\/code><\/pre>\n<p>\u5728\u6d4f\u89c8\u5668\u8bbf\u95ee <code>http:\/\/localhost:3000<\/code>\uff0c\u4f60\u4f1a\u770b\u5230\u54cd\u5e94\u3002\u8fd9\u4e2a\u670d\u52a1\u5668\u7684\u6027\u80fd\u6bd4 Node.js \u7684 Express \u9ad8\u5f88\u591a\uff0c\u56e0\u4e3a Bun \u5e95\u5c42\u4f7f\u7528\u539f\u751f\u4ee3\u7801\u5904\u7406\u8bf7\u6c42\u3002<\/p>\n<h2>\u751f\u6001\u6982\u89c8\uff1a\u73b0\u72b6\u4e0e\u6311\u6218<\/h2>\n<p>Bun \u7684\u751f\u6001\u6b63\u5728\u5feb\u901f\u53d1\u5c55\uff0c\u4f46\u76f8\u6bd4 Node.js \u4ecd\u6709\u5dee\u8ddd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u70b9\uff1a<\/p>\n<ul>\n<li><strong>\u6846\u67b6\u652f\u6301<\/strong>\uff1a\u4e3b\u6d41\u6846\u67b6\u5982 Next.js\u3001Nuxt \u5df2\u7ecf\u652f\u6301 Bun\uff08\u901a\u8fc7 <code>bun run dev<\/code>\uff09\uff0c\u4f46\u90e8\u5206\u8fb9\u7f18\u529f\u80fd\u53ef\u80fd\u4e0d\u517c\u5bb9\u3002<\/li>\n<li><strong>\u5de5\u5177\u94fe<\/strong>\uff1a\u50cf Prisma\u3001TypeORM \u7b49 ORM \u5728 Bun \u4e0b\u53ef\u80fd\u6709\u95ee\u9898\uff0c\u56e0\u4e3a Bun \u7684 Node.js \u517c\u5bb9\u5c42\u5e76\u975e 100%\u3002<\/li>\n<li><strong>\u539f\u751f\u6a21\u5757<\/strong>\uff1aBun \u652f\u6301\u901a\u8fc7 <code>bun build --compile<\/code> \u5c06\u5e94\u7528\u7f16\u8bd1\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f46\u4f9d\u8d56\u539f\u751f\u6a21\u5757\uff08\u5982 sharp\uff09\u65f6\u53ef\u80fd\u9700\u8981\u989d\u5916\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<p><strong>\ud83d\udca1 \u6211\u7684\u5efa\u8bae<\/strong>\uff1a\u5982\u679c\u4f60\u5728\u5f00\u53d1\u65b0\u9879\u76ee\uff0c\u4e14\u4f9d\u8d56\u8f83\u65b0\uff0c\u53ef\u4ee5\u5c1d\u8bd5 Bun\uff1b\u5982\u679c\u662f\u7ef4\u62a4\u8001\u9879\u76ee\uff0c\u5efa\u8bae\u5148\u5728 CI \u4e2d\u6d4b\u8bd5\u517c\u5bb9\u6027\u3002<\/p>\n<h2>\u5e38\u89c1\u5751\u4e0e\u8e29\u5751\u7ecf\u9a8c<\/h2>\n<ol>\n<li><strong>Node.js \u517c\u5bb9\u6027<\/strong>\uff1aBun \u5b9e\u73b0\u4e86\u5927\u90e8\u5206 Node.js API\uff0c\u4f46\u5e76\u975e\u5168\u90e8\u3002\u4f8b\u5982\uff0c<code>process.nextTick<\/code> \u7684\u884c\u4e3a\u7565\u6709\u4e0d\u540c\u3002<\/li>\n<li><strong>\u9501\u6587\u4ef6<\/strong>\uff1a<code>bun.lockb<\/code> \u662f\u4e8c\u8fdb\u5236\u7684\uff0c\u4e0d\u80fd\u50cf <code>package-lock.json<\/code> \u90a3\u6837\u76f4\u63a5\u9605\u8bfb\uff0c\u4f46\u4fdd\u8bc1\u4e86\u786e\u5b9a\u6027\u3002<\/li>\n<li><strong>\u5168\u5c40\u5b89\u88c5<\/strong>\uff1a<code>bun add -g<\/code> \u5b89\u88c5\u7684\u5168\u5c40\u5305\uff0c\u8def\u5f84\u4e0e npm \u4e0d\u540c\uff0c\u53ef\u80fd\u5bfc\u81f4\u67d0\u4e9b\u5de5\u5177\u627e\u4e0d\u5230\u3002<\/li>\n<li><strong>Windows \u652f\u6301<\/strong>\uff1aBun \u5728 Windows \u4e0a\u9700\u8981 WSL\uff0c\u539f\u751f Windows \u652f\u6301\u4ecd\u5728\u5f00\u53d1\u4e2d\u3002<\/li>\n<\/ol>\n<h2>\u603b\u7ed3\u4e0e\u4e0b\u4e00\u6b65<\/h2>\n<p>Bun \u4ee5\u5176\u6781\u81f4\u7684\u6027\u80fd\u548c\u4e00\u4f53\u5316\u7684\u5de5\u5177\u94fe\uff0c\u6b63\u5728\u6210\u4e3a JavaScript \u751f\u6001\u4e2d\u4e0d\u53ef\u5ffd\u89c6\u7684\u529b\u91cf\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b9e\u8df5\uff0c\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86 Bun \u7684\u5b89\u88c5\u3001\u5305\u7ba1\u7406\u3001\u6253\u5305\u3001\u6d4b\u8bd5\u548c\u670d\u52a1\u5668\u5f00\u53d1\u3002<\/p>\n<p>\u4e0b\u4e00\u6b65\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u73b0\u6709\u7684 Node.js \u9879\u76ee\u8fc1\u79fb\u5230 Bun\uff0c\u6216\u8005\u7528 Bun \u6784\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u5168\u6808\u5e94\u7528\u3002\u5173\u6ce8 Bun \u7684\u5b98\u65b9\u535a\u5ba2\u548c GitHub\uff0c\u4e86\u89e3\u6700\u65b0\u8fdb\u5c55\u3002<\/p>\n<p>\u5982\u679c\u4f60\u89c9\u5f97\u672c\u6587\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u70b9\u8d5e\u548c\u5206\u4eab\uff0c\u8ba9\u66f4\u591a\u4eba\u4e86\u89e3 Bun\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bun \u662f\u4e00\u4e2a\u96c6\u6253\u5305\u5668\u3001\u6d4b\u8bd5\u8fd0\u884c\u5668\u3001\u5305\u7ba1\u7406\u5668\u4e8e\u4e00\u4f53\u7684 JavaScript \u8fd0\u884c\u65f6\uff0c\u4ee5\u5176\u60ca\u4eba\u7684\u6027\u80fd\u8457\u79f0\u3002\u672c\u6587\u5c06\u4ece\u96f6\u5f00\u59cb\uff0c\u5e26\u4f60\u5feb\u901f\u4e0a\u624b Bun\uff0c\u5e76\u6df1\u5165\u63a2\u7d22\u5176\u6838\u5fc3\u529f\u80fd\u4e0e\u751f\u6001\u73b0\u72b6\uff0c\u52a9\u4f60\u8bc4\u4f30\u662f\u5426\u503c\u5f97\u8fc1\u79fb\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[189,37,40,191,190],"class_list":["post-229","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-bun","tag-javascript","tag-typescript","tag-191","tag-190"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=229"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/229\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}