Preface
Previously we mentioned how Astro builds pages, such as breaking a page into “components” and using Astro Islands to assemble pages. In this chapter we’ll actually create an Astro project!
Creating a project
Prerequisites
Astro provides a CLI tool to scaffold projects. Before creating a project, you’ll need the following environment:
- Node.js -
18.14.1or higher (consider using nvm to switch versions) - Text editor - VS Code with the official extension is recommended (or any other editor you prefer; see the official docs for details)
- Terminal
Create an Astro project with the automatic setup tool
npm create astro@latest# Install the following packages create-astro@4.0.1? Type y to continueNeed to install the following packages: create-astro@4.0.1Ok to proceed? (y) y
# Where should we create your new project? dir Where should we create your new project? ./project-name
# Use a project template? 1. Include sample files (recommended) 2. Use blog template 3. Empty tmpl How would you like to start your new project? > Include sample files (recommended) — Use blog template — Empty
# Install dependencies? (recommended) deps Install dependencies? (recommended) ● Yes ○ No
# Do you plan to use TypeScript? ts Do you plan to write TypeScript? ● Yes ○ No
# How strict should TypeScript be configured? 1. Strict (recommended) 2. Strictest 3. Relaxed use How strict should TypeScript be? > Strict (recommended) — Strictest — Relaxed
# Initialize a Git repo?git Initialize a new git repository? (optional) ● Yes ○ NoWith that, a complete Astro project is created. The overall project structure will look like this:
project├── .git # Git-related files├── .vscode # VS Code related files├── node_modules # NPM dependencies├── public # Files you don't want Astro to process│ └── favicon.svg├── src # Project source code│ ├── pages│ │ └── index.astro│ └── env.d.ts├── .gitignore├── astro.config.mjs # Astro config file├── package.json # NPM records├── README.md # Project description file└── tsconfig.json # TypeScript config filepublic/ folder
Used to store files you don’t want Astro to process. Contents are copied verbatim into the rendered output folder.
src/ folder
Used to store project source files. You can organize your source files into folders. Folder names are flexible; a common src structure looks like this:
src/├── components # Components├── content # Files related to Content Collections├── layouts # Layout components├── pages # Page components└── styles # Project stylesExcept for src/pages/ and src/content/, folder names can be whatever you prefer. Only the pages folder is required because files in it are used to create pages. Later chapters will explain their individual purposes; for now it’s enough to understand the general project layout.
Starting Astro
There are three common commands: start the dev server, build the site, and preview the built site. The rendered site is output to a dist folder by default.
# Start the development servernpm run dev
# Build the sitenpm run build
# Preview the built sitenpm run previewSummary
Creating an Astro project is very simple. The CLI quickly scaffolds a working project. If you’re familiar with Vite, the structure may feel familiar, since Astro uses Vite under the hood!
Further reading
- The Astro CLI - Astro DOCS
- Install Astro with the Automatic CLI - Astro DOCS
- Day3 - 建構新專案 - The same article is simultaneously published in the iThome Ironman Competition