The Astro CLI
หากคุณเคยรัน next dev คุณรู้ pattern นี้แล้ว Astro มาพร้อม binary astro ตัวเดียวที่จัดการทุกขั้นตอนของ development lifecycle — dev server, production build, local preview, integration scaffolding และ type checking โดยแมปกับคำสั่ง Next.js CLI ที่คุณใช้อยู่ทุกวันได้แทบทุกคำสั่ง
คำสั่งหลัก
หัวข้อที่มีชื่อว่า “คำสั่งหลัก”# Start the dev server (hot module reloading, port 4321 by default)npx astro dev
# Production build → dist/npx astro build
# Serve the dist/ folder locally (mirrors the production build exactly)npx astro preview
# Add an official integration and update astro.config.mjs automaticallynpx astro add reactnpx astro add tailwindnpx astro add sitemap
# Type-check all .astro and .ts filesnpx astro checkpackage.json scripts
หัวข้อที่มีชื่อว่า “package.json scripts”ทั้งสอง framework ใช้ convention เดียวกันในการตั้งชื่อย่อให้กับคำสั่ง CLI ใน scripts
// package.json — Next.js project{ "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint", "type-check": "tsc --noEmit" }}// package.json — Astro project{ "scripts": { "dev": "astro dev", "build": "astro build", "preview": "astro preview", "check": "astro check" }}สองสิ่งที่ควรสังเกต:
astro previewคือ local preview server — ไม่ใช่ production server จริง ใช้ adapter (Node, Vercel, Cloudflare) สำหรับ SSR production จริงๆastro checkแทนที่สองขั้นตอนtsc --noEmit+ linter ที่โปรเจกต์ Next.js มักต้องดูแลแยกกัน
คำสั่ง astro add
หัวข้อที่มีชื่อว่า “คำสั่ง astro add”npx astro add scaffold official integrations ในขั้นตอนเดียว: ติดตั้ง npm package, อัปเดต astro.config.mjs และพิมพ์สิ่งที่เปลี่ยนแปลง
# Add the React integration (enables .tsx islands)npx astro add react
# Add Tailwind CSSnpx astro add tailwind
# Add the Vercel SSR adapternpx astro add vercel
# Add multiple at oncenpx astro add react tailwind sitemapไม่มีสิ่งเทียบเท่าใน Next.js — ใน Next คุณต้อง npm install package แล้วอัปเดต next.config.js ด้วยตนเอง คำสั่ง astro add อัตโนมัติขั้นตอน config ให้
CLI flags
หัวข้อที่มีชื่อว่า “CLI flags”# Custom portastro dev --port 3000
# Bind to all interfaces (useful in Docker / WSL)astro dev --host
# Verbose output during buildastro build --verbose
# Watch mode for astro check (re-runs on file save)astro check --watchอัปเกรด: Astro 5 → 7
หัวข้อที่มีชื่อว่า “อัปเกรด: Astro 5 → 7”npx @astrojs/upgrade จะ bump astro และ integration @astrojs/* ทุกตัวไป major ล่าสุด จากนั้นไล่ตาม checklist นี้ (ครอบคลุม breaking change ทั้งของ v6 และ v7):
- ต้องใช้ Node 22.12+ (Astro 7 มาพร้อม Vite 8 และ bundler Rolldown)
- Content collections: ทุก collection ต้องมี
loader(glob()/file()จากastro/loaders);entry.slug→entry.id;entry.render()→render(entry); importzจากastro/zod - API ที่ถูกถอด:
Astro.glob()→import.meta.glob();<ViewTransitions />→<ClientRouter />;prefetch(url, { with })→prefetch(url); session driver ใช้ objectsessionDrivers.redis({...})ไม่ใช่ string - Config flag: ลบ
experimental.cache,experimental.routeRules,experimental.rustCompiler,experimental.queuedRendering,experimental.advancedRouting,experimental.loggerออก — ตอนนี้เป็น top-level หรือเป็นค่าเริ่มต้นแล้ว - Template: แก้ tag ที่ไม่ปิดหรือซ้อนผิด (compiler ที่เขียนด้วย Rust จะ error แทนการแก้ให้อัตโนมัติ) และเติม
{" "}ในจุดที่เคยพึ่ง whitespace จากการขึ้นบรรทัดใหม่ (compressHTML: 'jsx'เป็นค่าเริ่มต้น) - Markdown: Sätteri เป็น processor ค่าเริ่มต้น ถ้าจะใช้ remark/rehype plugin ต่อให้ตั้ง
markdown.processor: unified()จาก@astrojs/markdown-remark - หายไปถาวร:
@astrojs/db(ใช้node:sqlite, Drizzle หรือ DB แบบ hosted แทน) และ event constant ของastro:transitions(ใช้ชื่อ event แบบ string)