What it is
The Netlify CLI allows you to deploy and manage your Netlify sites directly from your command line, enabling local development with live previews and seamless deployments.
Installation
Linux / macOS:
npm install netlify-cli -g
Windows:
npm install netlify-cli -g
(Requires Node.js and npm to be installed.)
Core Concepts
- Site: A project deployed to Netlify. Each site has a unique name and can be associated with a Git repository.
- Deploy: A specific version of your site deployed to Netlify. Deploys can be linked to Git commits.
- Context: The environment in which a command is run. The CLI supports
productionanddeploy-previewcontexts, as well as arbitrary custom contexts. - Netlify.toml: A configuration file that lives in the root of your project. It allows you to define build settings, environment variables, redirects, and more, making your deployments reproducible and portable.
Commands / Usage
Authentication
-
Log in to Netlify:
netlify loginOpens a browser window to authenticate your Netlify account.
-
Log out of Netlify:
netlify logoutLogs out of your Netlify account on the CLI.
Site Management
-
Link a local directory to a Netlify site:
netlify linkPrompts you to choose an existing site or create a new one to associate with your current directory.
-
Unlink a local directory from a Netlify site:
netlify unlinkRemoves the association between the current directory and its linked Netlify site.
-
View linked site information:
netlify statusShows the current Netlify site linked to your directory, its ID, and recent deploys.
Local Development and Preview
-
Start a local development server with live previews:
netlify devStarts a local server, watches for file changes, and deploys previews to Netlify. Uses your
netlify.tomlfor configuration.-
Specify a different port:
netlify dev --port 8888Starts the development server on port 8888.
-
Specify the directory to serve:
netlify dev --dir publicServes files from the
publicdirectory. -
Specify the build command:
netlify dev --command "npm run build"Runs
npm run buildbefore starting the server. -
Set environment variables for local development:
netlify dev --env MY_VARIABLE=my_value --env ANOTHER_VAR=another_valueSets environment variables for the local development server.
-
Deploying to Netlify
-
Deploy the current directory to Netlify:
netlify deployInitiates a deployment. Prompts for site selection and deploy type (draft or production).
-
Deploy as a draft (preview):
netlify deploy --draftCreates a deployable preview URL that is not the live production version.
-
Deploy to production:
netlify deploy --prodDeploys the current version of your site to your production URL.
-
Deploy with a specific message:
netlify deploy --message "Deploying new feature X"Adds a descriptive message to the deploy log.
-
Deploy from a specific directory:
netlify deploy --dir dist --prodDeploys the contents of the
distdirectory as a production build. -
Deploy with specific build settings (overriding
netlify.toml):netlify deploy --build --command "yarn build:prod" --functions src/functionsRuns a custom build command and specifies a functions directory.
Managing Deploys
-
List deploys for the linked site:
netlify deploysShows a list of recent deploys for the current Netlify site.
-
Open a specific deploy in the browser:
netlify deploys --openOpens the most recent deploy in your browser.
-
Open a production deploy in the browser:
netlify deploys --prod --openOpens the current production deploy in your browser.
-
Set a specific deploy as production:
netlify deploy --prod --id <deploy-id>Promotes a specific deploy ID to production.
-
List deploy previews:
netlify deploy --list-draftsShows all deploy previews.
-
Delete a deploy:
netlify deploy --delete --id <deploy-id>Deletes a specific deploy.
Environment Variables
-
View environment variables for the linked site:
netlify envDisplays environment variables for the currently linked Netlify site.
-
Set environment variables for a site:
netlify env:set MY_API_KEY YOUR_SECRET_KEY --site <site-id>Sets a specific environment variable for a given site ID.
-
Unset environment variables for a site:
netlify env:unset MY_API_KEY --site <site-id>Removes a specific environment variable from a given site ID.
-
Set environment variables for a specific context:
netlify env:set MY_CONFIG production --context productionSets
MY_CONFIGtoproductionspecifically for theproductioncontext.
Functions
- Deploy Netlify Functions:
Deploys functions located in the specified directory. This is often handled automatically bynetlify deploy --functions <path-to-functions-dir>netlify devandnetlify deployif configured innetlify.toml.
Other Utilities
-
Get help for a command:
netlify help <command>Provides detailed help for a specific Netlify CLI command.
-
View Netlify CLI configuration:
netlify configShows the current Netlify CLI configuration.
Common Patterns
-
Local development with a specific build command and output directory:
netlify dev --command "npm run build:dev" --dir buildStarts local development, running your dev build command and serving from the
builddirectory. -
Deploying a static site generator (e.g., Hugo): Assuming
netlify.tomlis configured withcommand = "hugo"andpublish = "public":netlify deploy --prodThis will build and deploy your Hugo site to production.
-
Deploying a React app built with Create React App: Assuming
netlify.tomlis configured withcommand = "npm run build"andpublish = "build":netlify deploy --prodThis will build and deploy your React app to production.
-
Setting a production deploy with a Git branch: If your Netlify site is linked to a Git repository, pushing to
mainmight automatically trigger a production deploy. Manually:netlify deploy --prod --branch mainThis command is more for manual CLI deployments tied to a branch, though Netlify’s Git integration often handles this automatically.
-
Managing environment variables for different contexts:
netlify env:set STRIPE_SECRET_KEY test_sk_123 --context dev netlify env:set STRIPE_SECRET_KEY live_sk_abc --context production netlify deploy --prodSets different Stripe secret keys for development and production contexts and then deploys to production.
-
Viewing logs for a specific deploy:
netlify logs <deploy-id>Retrieves build logs for a specific deploy ID.
Gotchas
netlify devvs.netlify deploy:netlify devis for local development and testing, providing live previews and a local server.netlify deploypushes your site to Netlify.netlify.tomlprecedence: Settings innetlify.tomlgenerally override command-line flags for build commands and publish directories, making it the source of truth for your build.- Contextual Environment Variables: Environment variables set for specific contexts (e.g.,
production,deploy-preview) are only active in those contexts.netlify devmight use a default or development context unless explicitly configured. - Site Linking: Ensure you are in the correct directory when running
netlify linkornetlify deploy. If you run these commands in a directory not associated with a Netlify site, you’ll be prompted to link or create one. - Global vs. Local Installation: While installing
netlify-cliglobally (-g) is common, it’s good practice to also have it as a dev dependency in yourpackage.json(npm install netlify-cli --save-dev) for project-specific versioning and consistency. You can then run commands usingnpx netlify <command>. - Build Output Directory: If
netlify.tomlspecifies apublishdirectory,netlify deploywill upload the contents of that directory. Ifnetlify devis used without specifying adirflag, it often infers this fromnetlify.tomlor a default likepublicordist.