Angular Netlify Deployment
Introduction
Deploying your Angular application to a production environment is a crucial step in the development process. Netlify offers a straightforward, developer-friendly platform that makes deployment simple with features like continuous deployment, custom domains, and HTTPS by default.
In this tutorial, you'll learn how to deploy your Angular application to Netlify, configure the deployment settings, and set up continuous deployment from your Git repository.
What is Netlify?
Netlify is a modern web hosting and automation platform that specializes in static site hosting and serverless functions. It offers:
- Continuous deployment directly from Git repositories
- Global CDN for fast content delivery
- Automatic HTTPS with Let's Encrypt
- Form handling, identity management, and serverless functions
- Branch-based previews for testing changes
Prerequisites
Before you begin, make sure you have:
- An Angular project ready for deployment
- A GitHub, GitLab, or Bitbucket account with your project code
- A Netlify account (you can sign up for free at netlify.com)
Preparing Your Angular Application for Netlify
Step 1: Add a Netlify Configuration File
Create a file named netlify.toml
in the root of your Angular project:
[build]
publish = "dist/your-project-name"
command = "ng build"
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
This configuration tells Netlify:
- How to build your application (
ng build
) - Where to find the built files (
dist/your-project-name
) - To redirect all routes to
index.html
, which is necessary for Angular's client-side routing
Step 2: Ensure You Have a Production Build Configuration
Make sure your angular.json
file has a proper production configuration:
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
Deploying to Netlify
Method 1: Connecting to Git Repository (Recommended)
This method enables continuous deployment - Netlify will automatically rebuild your site whenever you push changes to your repository.
-
Login to Netlify:
Go to Netlify and log in with your account.
-
Create a New Site:
Click on the "New site from Git" button.
-
Connect to Your Git Provider:
Select your Git provider (GitHub, GitLab, or Bitbucket) and authorize Netlify to access your repositories.
-
Select Your Repository:
Choose the repository containing your Angular project.
-
Configure Build Settings:
Enter the following settings:
- Build command:
ng build --prod
(ornpm run build
if you use npm scripts) - Publish directory:
dist/your-project-name
(replace with your actual project name)
- Build command:
-
Deploy the Site:
Click "Deploy site" to start the deployment process.
Method 2: Manual Deployment
If you prefer to deploy manually or want to test a specific build:
-
Build Your Angular Application:
Run the build command locally:
bashng build --prod
-
Deploy via Netlify UI:
- Go to the Netlify dashboard
- Drag and drop your
dist/your-project-name
folder onto the designated area - Netlify will automatically detect and deploy your site
Method 3: Using Netlify CLI
You can also use Netlify's command-line interface for deployments:
-
Install Netlify CLI:
bashnpm install -g netlify-cli
-
Login to Netlify:
bashnetlify login
-
Initialize Netlify in Your Project:
bashnetlify init
-
Deploy to Netlify:
bashnetlify deploy
For production deployment:
bashnetlify deploy --prod
Customizing Your Netlify Deployment
Custom Domain Setup
- In your Netlify dashboard, go to "Site settings" > "Domain management"
- Click "Add custom domain"
- Follow the instructions to set up DNS records with your domain provider
Environment Variables
For configuration values that shouldn't be in your code:
- Go to "Site settings" > "Build & deploy" > "Environment"
- Add your variables (like API keys) that can be accessed via your Angular environment files
Deploy Previews for Pull Requests
Netlify automatically creates preview deployments for pull requests, allowing you to test changes before merging:
- Make sure your repository is connected to Netlify
- Create a pull request in your Git repository
- Netlify will comment on the PR with a link to the preview
Real-World Example: Deploying an Angular Blog
Let's walk through deploying a simple Angular blog application to Netlify with continuous deployment from GitHub.
1. Project Structure
my-angular-blog/
├── src/
│ ├── app/
│ ├── assets/
│ ├── environments/
│ │ ├── environment.ts
│ │ └── environment.prod.ts
│ └── ...
├── angular.json
├── package.json
└── netlify.toml
2. Configure environment.prod.ts for Production Settings
export const environment = {
production: true,
apiUrl: 'https://api.yourblog.com/v1'
};
3. Set Up netlify.toml
[build]
publish = "dist/my-angular-blog"
command = "ng build --configuration production"
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
[context.production.environment]
ANGULAR_ENV = "production"
[context.deploy-preview.environment]
ANGULAR_ENV = "staging"
4. Create a Build Script in package.json
"scripts": {
"start": "ng serve",
"build": "ng build --configuration production",
"deploy:netlify": "ng build --configuration production && netlify deploy --prod"
}
5. Connecting to GitHub and Deploying
- Push your code to GitHub
- Connect your repository to Netlify as described earlier
- Configure build settings:
- Build command:
npm run build
- Publish directory:
dist/my-angular-blog
- Build command:
- Deploy the site
6. Set Up Environment Variables
For our blog API key that shouldn't be in the code:
- Go to Site settings > Environment variables
- Add a variable:
API_KEY
with your secret key - Access it in your Angular application through a serverless function
Troubleshooting Common Issues
1. Routing Issues
Problem: 404 errors when refreshing pages with Angular routes
Solution: Ensure you have the correct redirect rule in your netlify.toml
:
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
2. Build Failures
Problem: The build fails on Netlify but works locally
Solution:
- Check Netlify logs for specific errors
- Verify your Node.js version in a
.nvmrc
file:
14.17.0
3. Environment Variables Not Working
Problem: Your application can't access environment variables
Solution:
- Remember that environment variables are only available during build time
- Use Netlify functions for secrets that need to be accessed during runtime
Summary
In this tutorial, you've learned how to deploy an Angular application to Netlify using different methods, including:
- Continuous deployment from Git repositories
- Manual deployments for quick testing
- Using Netlify CLI for more control
You've also learned how to:
- Configure your Angular application for production deployment
- Set up environment variables and custom domains
- Handle common routing issues with single-page applications
- Create preview deployments for testing changes
Netlify provides an excellent platform for hosting Angular applications with minimal configuration and powerful features like continuous deployment, HTTPS by default, and serverless functions.
Additional Resources
- Netlify Documentation
- Angular Deployment Guide
- Netlify CLI Documentation
- Angular Universal for Server-Side Rendering
Exercises
- Deploy a simple Angular application to Netlify using the Git repository method.
- Set up a custom domain for your deployed Angular application.
- Create a form in your Angular application and configure it to work with Netlify Forms.
- Implement a serverless function using Netlify Functions to securely access an API from your Angular application.
- Set up branch deployments to have separate development, staging, and production environments.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)