Cài đặt Astro Local
Cài đặt Astro Local
Section titled “Cài đặt Astro Local”Astro là một static site generator hiện đại, tập trung vào performance và developer experience. Bài viết này hướng dẫn chi tiết cách cài đặt Astro trên máy tính local của bạn.
Yêu cầu hệ thống
Section titled “Yêu cầu hệ thống”Trước khi cài đặt Astro, bạn cần đảm bảo máy tính đã có:
- Node.js phiên bản 18.14.1 trở lên
- npm (đi kèm với Node.js)
- Một code editor (VS Code khuyến nghị)
Cách 1: Tạo project mới với CLI
Section titled “Cách 1: Tạo project mới với CLI”Đây là cách đơn giản nhất để bắt đầu với Astro.
Bước 1: Chạy lệnh tạo project
Section titled “Bước 1: Chạy lệnh tạo project”# Tạo project mớinpm create astro@latest my-astro-project
# Hoặc dùng yarnyarn create astro my-astro-project
# Hoặc dùng pnpmpnpm create astro@latest my-astro-projectBước 2: Trả lờI các câu hỏi
Section titled “Bước 2: Trả lờI các câu hỏi”CLI sẽ hỏi bạn một số câu hỏi:
| Câu hỏi | Gợi ý |
|---|---|
| How would you like to start? | Chọn “A few best practices” cho beginners |
| Install dependencies? | Chọn Yes |
| Initialize a git repository? | Tùy chọn, nên chọn Yes |
| TypeScript? | Nên chọn Yes |
Bước 3: Chạy dev server
Section titled “Bước 3: Chạy dev server”cd my-astro-projectnpm run devTruy cập http://localhost:4321 để xem kết quả.
💡 Pro Tip
Thêm flag-- --port 3000để chạy ở port khác nếu 4321 đã bị chiếm:Terminal window npm run dev -- --port 3000
Cách 2: Tạo project thủ công
Section titled “Cách 2: Tạo project thủ công”Nếu muốn hiểu rõ hơn cấu trúc project, bạn có thể tạo thủ công.
Bước 1: Tạo folder và init npm
Section titled “Bước 1: Tạo folder và init npm”mkdir my-astro-projectcd my-astro-projectnpm init -yBước 2: Cài đặt Astro
Section titled “Bước 2: Cài đặt Astro”npm install astroBước 3: Tạo cấu trúc thư mục
Section titled “Bước 3: Tạo cấu trúc thư mục”my-astro-project/├── src/│ ├── pages/│ │ └── index.astro│ └── components/├── public/├── astro.config.mjs└── package.jsonBước 4: Tạo file cấu hình
Section titled “Bước 4: Tạo file cấu hình”astro.config.mjs:
import { defineConfig } from 'astro/config';
export default defineConfig({ // Cấu hình của bạn ở đây});Bước 5: Tạo trang đầu tiên
Section titled “Bước 5: Tạo trang đầu tiên”src/pages/index.astro:
---const title = "Astro của tôi";---
<html lang="vi"> <head> <meta charset="utf-8" /> <title>{title}</title> </head> <body> <h1>Chào mừng đến với Astro!</h1> <p>Đây là trang web đầu tiên của tôi.</p> </body></html>Bước 6: Thêm scripts vào package.json
Section titled “Bước 6: Thêm scripts vào package.json”{ "scripts": { "dev": "astro dev", "build": "astro build", "preview": "astro preview" }}Bước 7: Chạy server
Section titled “Bước 7: Chạy server”npm run devCấu trúc project Astro
Section titled “Cấu trúc project Astro”my-astro-project/├── src/ # Source code│ ├── components/ # UI components (có thể tái sử dụng)│ ├── layouts/ # Page layouts│ ├── pages/ # File-based routing│ └── content/ # Markdown/MDX content (blog, docs)├── public/ # Static assets (images, fonts)├── astro.config.mjs # Cấu hình Astro├── tsconfig.json # Cấu hình TypeScript└── package.json # DependenciesLệnh npm thường dùng
Section titled “Lệnh npm thường dùng”| Lệnh | Mô tả |
|---|---|
npm run dev | Chạy dev server với hot reload |
npm run build | Build cho production |
npm run preview | Preview build production |
npm run astro -- --help | Xem tất cả options |
Lỗi thường gặp và cách fix
Section titled “Lỗi thường gặp và cách fix”Lỗi 1: Port đã được sử dụng
Section titled “Lỗi 1: Port đã được sử dụng”Error: Port 4321 is already in useFix:
# Kill process đang dùng portnpx kill-port 4321
# Hoặc chạy ở port khácnpm run dev -- --port 3000Lỗi 2: “Cannot find module”
Section titled “Lỗi 2: “Cannot find module””Fix:
# Xóa node_modules và cài lạirm -rf node_modules package-lock.jsonnpm installLỗi 3: TypeScript errors
Section titled “Lỗi 3: TypeScript errors”Fix: Cài đặt types:
npm install --save-dev @types/nodeTích hợp với VS Code
Section titled “Tích hợp với VS Code”Astro có extension chính thức cho VS Code:
- Mở Extensions (Ctrl+Shift+X)
- Tìm “Astro”
- Cài đặt extension của Astro
Extension cung cấp:
- Syntax highlighting
- IntelliSense
- Auto-formatting
Next Steps
Section titled “Next Steps”Next Steps
Section titled “Next Steps”Sau khi cài đặt xong, bạn có thể:
- Học cú pháp Astro (.astro files)
- Tích hợp React/Vue/Svelte components
- Thêm Tailwind CSS
- Deploy lên Vercel/Netlify
Chúc mừng!
Bạn đã cài đặt thành công Astro! Giờ có thể bắt đầu xây dựng website tĩnh siêu nhanh của mình.