Using the CLI
Use the CLI by running one of the commands documented on this page with your preferred package manager, optionally followed by any flags. Flags customize the behavior of a command.npm
pnpm
yarn
Available Commands
astro dev
Runs Astro’s development server. This is a local HTTP server that doesn’t bundle assets. It uses Hot Module Replacement (HMR) to update your browser as you save changes in your editor.
Keyboard shortcuts:
s + enter- sync the content layer data (content and types)o + enter- open your Astro site in the browserq + enter- quit the development server
Specify which port to run on.
Listen on all addresses, including LAN and public addresses. Pass a custom address to expose on a specific network IP.
Automatically open the app in the browser on server start. Can be passed a full URL or pathname.
Clear the content layer cache, forcing a full rebuild.
astro build
Builds your site for deployment. By default, this will generate static files and place them in a dist/ directory. If any routes are rendered on demand, this will generate the necessary server files to serve your site.
Example:
Outputs a development-based build similar to code transformed in
astro dev. This can be useful to test build-only issues with additional debugging information included.astro preview
Starts a local server to serve the contents of your static directory (dist/ by default) created by running astro build.
This command allows you to preview your site locally after building to catch any errors in your build output before deploying it. It is not designed to be run in production.
Keyboard shortcuts:
o + enter- open your Astro site in the browserq + enter- quit the preview server
astro check
Runs diagnostics (such as type-checking within .astro files) against your project and reports errors to the console. If any errors are found the process will exit with a code of 1.
This command is intended to be used in CI workflows.
Example:
The command will watch for any changes in your project, and will report any errors.
Specifies a different root directory to check. Uses the current working directory by default.
Specifies a
tsconfig.json file to use manually. If not provided, Astro will attempt to find a config, or infer the project’s config automatically.Specifies the minimum severity needed to exit with an error code.
Specifies the minimum severity to output.
Specifies not to clear the output between checks when in watch mode.
Specifies not to run
astro sync before checking the project.astro sync
Generates TypeScript types for all Astro modules. This sets up a .astro/types.d.ts file for type inferencing, and defines modules for features that rely on generated types:
- The
astro:contentmodule for the Content Collections API - The
astro:dbmodule for Astro DB - The
astro:envmodule for Astro Env - The
astro:actionsmodule for Astro Actions
Running
astro dev, astro build or astro check will run the sync command as well.astro add
Adds an integration to your configuration. This command will:
- Install the necessary dependencies
- Update your
astro.config.mjsfile - Apply any necessary configuration changes
astro docs
Launches the Astro Docs website directly from the terminal.
Example:
astro info
Reports useful information about your current Astro environment. Useful for providing information when opening an issue.
Example:
The command will copy the output to the clipboard without prompting.
astro preferences
Manage user preferences with the astro preferences command. User preferences are specific to individual Astro users, unlike the astro.config.mjs file which changes behavior for everyone working on a project.
User preferences are scoped to the current project by default, stored in a local .astro/settings.json file. Using the --global flag, user preferences can also be applied to every Astro project on the current machine.
Available preferences:
devToolbar- Enable or disable the development toolbar in the browser (Default:true)checkUpdates- Enable or disable automatic update checks for the Astro CLI (Default:true)
astro telemetry
Sets telemetry configuration for the current CLI user. Telemetry is anonymous data that provides the Astro team insights into which Astro features are most often used.
Examples:
astro create-key
Generates a key to encrypt props passed to server islands.
Example:
ASTRO_KEY environment variable (e.g. in a .env file) and include it in your CI/CD or host’s build settings when you need a constant encryption key for your server islands for situations like rolling deployments, multi-region hosting or a CDN that caches pages containing server islands.
Global Flags
These flags can be used with any command.Specifies the path to the project root. If not specified, the current working directory is assumed to be the root.
Specifies the path to the config file relative to the project root. Defaults to
astro.config.mjs. Use this if you use a different name for your configuration file or have your config file in another folder.Configures the
site for your project. Passing this flag will override the site value in your astro.config.mjs file, if one exists.Configures the
base for your project. Passing this flag will override the base value in your astro.config.mjs file, if one exists.Specifies which port to run the dev server and preview server on.
Sets which network IP addresses the dev server and preview server should listen on (i.e. non-localhost IPs).
--host- listen on all addresses, including LAN and public addresses--host <custom-address>- expose on a network IP address at<custom-address>
Specifies the hostnames that Astro is allowed to respond to in
dev or preview modes. Can be passed a comma-separated list of hostnames or true to allow any hostname.Automatically opens the app in the browser on server start. Can be passed a full URL string or a pathname.
Configures the
outDir for your project. Passing this flag will override the outDir value in your astro.config.mjs file, if one exists.Clear the content layer cache, forcing a full rebuild.
Configures the
mode inline config for your project.Enables verbose logging, which is helpful when debugging an issue.
Enables silent logging, which will run the server without any console output.
Prints the Astro version number and exits.
Prints the help message and exits.
package.json Scripts
You can also use scripts inpackage.json for shorter versions of these commands. Using a script allows you to use the same commands that you may be familiar with from other projects.
The following scripts for the most common astro commands are added for you automatically when you create a project using the create astro wizard:
package.json
-- before flags:
npm
pnpm
yarn