How do you deploy Angular apps?
Solution 1:
Simple answer. Use the Angular CLI and issue the
ng build
command in the root directory of your project. The site will be created in the dist directory and you can deploy that to any web server.
This will build for test, if you have production settings in your app you should use
ng build --configuration production
This will build the project in the dist
directory and this can be pushed to the server.
Much has happened since I first posted this answer. The CLI is finally at a 1.0.0 so following this guide go upgrade your project should happen before you try to build. https://github.com/angular/angular-cli/wiki/stories-rc-update
Solution 2:
You are actually here touching two questions in one.
The first one is How to host your application?.
And as @toskv mentioned its really too broad question to be answered and depends on numerous different things.
The second one is How do you prepare the deployment version of the application?.
You have several options here:
- Deploy as it is. Just that - no minification, concatenation, name mangling, etc. Transpile all your ts project copy all your resulting js/css/... sources + dependencies to the hosting server and you are good to go.
Deploy using special bundling tools, like
webpack
orsystemjs
builder.
They come with all the possibilities that are lacking in #1.
You can pack all your app code into just a couple of js/css/... files that you reference in your HTML.systemjs
builder even allows you to get rid of the need to includesystemjs
as part of your deployment package.You can use
ng deploy
as of Angular 8 to deploy your app from your CLI.ng deploy
will need to be used in conjunction with your platform of choice (such as@angular/fire
). You can check the official docs to see what works best for you here
Yes you will most likely need to deploy systemjs
and bunch of other external libraries as part of your package. And yes you will be able to bundle them into just couple of js files you reference from your HTML page.
You do not have to reference all your compiled js files from the page though - systemjs
as a module loader will take care of that.
I know it sounds muddy - to help get you started with the #2 here are two really good sample applications:
SystemJS builder: angular2 seed
WebPack: angular2 webpack starter
Solution 3:
With the Angular CLI it's easy. An example for Heroku:
Create a Heroku account and install the CLI
Move the
angular-cli
dep to thedependencies
inpackage.json
(so that it gets installed when you push to Heroku.Add a
postinstall
script that will runng build
when the code gets pushed to Heroku. Also add a start command for a Node server that will be created in the following step. This will place the static files for the app in adist
directory on the server and start the app afterward.
"scripts": {
// ...
"start": "node server.js",
"postinstall": "ng build --aot -prod"
}
- Create an Express server to serve the app.
// server.js
const express = require('express');
const app = express();
// Run the app by serving the static files
// in the dist directory
app.use(express.static(__dirname + '/dist'));
// Start the app by listening on the default
// Heroku port
app.listen(process.env.PORT || 8080);
- Create a Heroku remote and push to depoy the app.
heroku create
git add .
git commit -m "first deploy"
git push heroku master
Here's a quick writeup I did that has more detail, including how to force requests to use HTTPS and how to handle PathLocationStrategy
:)