bun-next.js by secondsky/claude-skills
npx skills add https://github.com/secondsky/claude-skills --skill 'Bun Next.js'使用 Bun 运行 Next.js 应用程序,以获得更快的开发和构建速度。
# 使用 Bun 创建新的 Next.js 项目
bunx create-next-app@latest my-app
cd my-app
# 安装依赖
bun install
# 开发模式
bun run dev
# 构建
bun run build
# 生产模式
bun run start
{
"scripts": {
"dev": "next dev --turbo",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "^16.1.1",
"react": "^19.2.3",
"react-dom": "^19.2.3"
}
}
{
"scripts": {
"dev": "bun --bun next dev",
"build": "bun --bun next build",
"start": "bun --bun next start"
}
}
--bun 标志强制 Next.js 使用 Bun 的运行时而不是 Node.js。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
/** @type {import('next').NextConfig} */
const nextConfig = {
// 启用实验性功能
experimental: {
// Turbopack(更快的开发)
turbo: {},
},
// 服务器端 Bun API
serverExternalPackages: ["bun:sqlite"],
// Webpack 配置(如果需要)
webpack: (config, { isServer }) => {
if (isServer) {
// 允许 Bun 特定的导入
config.externals.push("bun:sqlite", "bun:ffi");
}
return config;
},
};
module.exports = nextConfig;
// app/page.tsx (服务器组件)
import { Database } from "bun:sqlite";
export default async function Home() {
const db = new Database("data.sqlite");
const users = db.query("SELECT * FROM users").all();
db.close();
return (
<div>
{users.map((user) => (
<p key={user.id}>{user.name}</p>
))}
</div>
);
}
// app/api/users/route.ts
import { Database } from "bun:sqlite";
export async function GET() {
const db = new Database("data.sqlite");
const users = db.query("SELECT * FROM users").all();
db.close();
return Response.json(users);
}
export async function POST(request: Request) {
const body = await request.json();
const db = new Database("data.sqlite");
db.run("INSERT INTO users (name) VALUES (?)", [body.name]);
db.close();
return Response.json({ success: true });
}
// app/api/files/route.ts
export async function GET() {
const file = Bun.file("./data/config.json");
const config = await file.json();
return Response.json(config);
}
export async function POST(request: Request) {
const data = await request.json();
await Bun.write("./data/config.json", JSON.stringify(data, null, 2));
return Response.json({ saved: true });
}
// app/actions.ts
"use server";
import { Database } from "bun:sqlite";
import { revalidatePath } from "next/cache";
export async function createUser(formData: FormData) {
const name = formData.get("name") as string;
const db = new Database("data.sqlite");
db.run("INSERT INTO users (name) VALUES (?)", [name]);
db.close();
revalidatePath("/users");
}
export async function deleteUser(id: number) {
const db = new Database("data.sqlite");
db.run("DELETE FROM users WHERE id = ?", [id]);
db.close();
revalidatePath("/users");
}
// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
// 检查认证
const token = request.cookies.get("token");
if (!token && request.nextUrl.pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*"],
};
# .env.local
DATABASE_URL=./data.sqlite
API_SECRET=your-secret-key
// 在服务器组件/操作中访问
const dbUrl = process.env.DATABASE_URL;
const secret = process.env.API_SECRET;
// 暴露给客户端(使用 NEXT_PUBLIC_ 前缀)
// .env.local
NEXT_PUBLIC_API_URL=https://api.example.com
bun run build
bun run start
FROM oven/bun:1
WORKDIR /app
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile
COPY . .
RUN bun run build
EXPOSE 3000
CMD ["bun", "run", "start"]
# 安装 Vercel CLI
bun add -g vercel
# 部署
vercel
注意:Vercel 的边缘运行时使用 V8,而不是 Bun。Bun API 在以下环境中工作:
使用 Turbopack 以获得更快的开发速度:
bun run dev --turbo
优先使用服务器组件 - 发送到客户端的 JavaScript 更少
对于简单应用,使用 Bun SQLite 而不是外部数据库
启用压缩:
// next.config.js
module.exports = {
compress: true,
};
| 错误 | 原因 | 解决方法 |
|---|---|---|
Cannot find bun:sqlite | 运行时错误 | 使用 bun --bun next dev |
Module not found | 缺少依赖 | 运行 bun install |
Hydration mismatch | 服务器/客户端数据不一致 | 检查数据获取 |
Edge runtime error | 在边缘运行时使用 Bun API | 使用 Node.js 运行时 |
加载 references/app-router.md 当需要:
加载 references/caching.md 当需要:
每周安装次数
–
代码仓库
GitHub 星标数
93
首次出现
–
Run Next.js applications with Bun for faster development and builds.
# Create new Next.js project with Bun
bunx create-next-app@latest my-app
cd my-app
# Install dependencies
bun install
# Development
bun run dev
# Build
bun run build
# Production
bun run start
{
"scripts": {
"dev": "next dev --turbo",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "^16.1.1",
"react": "^19.2.3",
"react-dom": "^19.2.3"
}
}
{
"scripts": {
"dev": "bun --bun next dev",
"build": "bun --bun next build",
"start": "bun --bun next start"
}
}
The --bun flag forces Next.js to use Bun's runtime instead of Node.js.
/** @type {import('next').NextConfig} */
const nextConfig = {
// Enable experimental features
experimental: {
// Turbopack (faster dev)
turbo: {},
},
// Server-side Bun APIs
serverExternalPackages: ["bun:sqlite"],
// Webpack config (if needed)
webpack: (config, { isServer }) => {
if (isServer) {
// Allow Bun-specific imports
config.externals.push("bun:sqlite", "bun:ffi");
}
return config;
},
};
module.exports = nextConfig;
// app/page.tsx (Server Component)
import { Database } from "bun:sqlite";
export default async function Home() {
const db = new Database("data.sqlite");
const users = db.query("SELECT * FROM users").all();
db.close();
return (
<div>
{users.map((user) => (
<p key={user.id}>{user.name}</p>
))}
</div>
);
}
// app/api/users/route.ts
import { Database } from "bun:sqlite";
export async function GET() {
const db = new Database("data.sqlite");
const users = db.query("SELECT * FROM users").all();
db.close();
return Response.json(users);
}
export async function POST(request: Request) {
const body = await request.json();
const db = new Database("data.sqlite");
db.run("INSERT INTO users (name) VALUES (?)", [body.name]);
db.close();
return Response.json({ success: true });
}
// app/api/files/route.ts
export async function GET() {
const file = Bun.file("./data/config.json");
const config = await file.json();
return Response.json(config);
}
export async function POST(request: Request) {
const data = await request.json();
await Bun.write("./data/config.json", JSON.stringify(data, null, 2));
return Response.json({ saved: true });
}
// app/actions.ts
"use server";
import { Database } from "bun:sqlite";
import { revalidatePath } from "next/cache";
export async function createUser(formData: FormData) {
const name = formData.get("name") as string;
const db = new Database("data.sqlite");
db.run("INSERT INTO users (name) VALUES (?)", [name]);
db.close();
revalidatePath("/users");
}
export async function deleteUser(id: number) {
const db = new Database("data.sqlite");
db.run("DELETE FROM users WHERE id = ?", [id]);
db.close();
revalidatePath("/users");
}
// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
// Check auth
const token = request.cookies.get("token");
if (!token && request.nextUrl.pathname.startsWith("/dashboard")) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*"],
};
# .env.local
DATABASE_URL=./data.sqlite
API_SECRET=your-secret-key
// Access in server components/actions
const dbUrl = process.env.DATABASE_URL;
const secret = process.env.API_SECRET;
// Expose to client (prefix with NEXT_PUBLIC_)
// .env.local
NEXT_PUBLIC_API_URL=https://api.example.com
bun run build
bun run start
FROM oven/bun:1
WORKDIR /app
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile
COPY . .
RUN bun run build
EXPOSE 3000
CMD ["bun", "run", "start"]
# Install Vercel CLI
bun add -g vercel
# Deploy
vercel
Note: Vercel's edge runtime uses V8, not Bun. Bun APIs work in:
Use Turbopack for faster dev:
bun run dev --turbo
Prefer Server Components - Less JavaScript sent to client
Use Bun SQLite instead of external databases for simple apps
Enable compression :
// next.config.js
module.exports = {
compress: true,
};
| Error | Cause | Fix |
|---|---|---|
Cannot find bun:sqlite | Wrong runtime | Use bun --bun next dev |
Module not found | Missing dependency | Run bun install |
Hydration mismatch | Server/client diff | Check data fetching |
Edge runtime error | Bun API on edge |
Load references/app-router.md when:
Load references/caching.md when:
Weekly Installs
–
Repository
GitHub Stars
93
First Seen
–
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
109,600 周安装
| Use Node.js runtime |