This website collects cookies to deliver better user experience
How to make Angular CLI built app work from a subfolder
How to make Angular CLI built app work from a subfolder
Angular CLI, along with many boilerplate generating tools for other frameworks, insists on outputting the built code in a way that will work only on the top-level domain. I can't understand the reason for this, and definitively is not working well with quick&drity example apps you would like to show anywhere easily.
In this article, I'll show you how to make angular build code in a way that you can just drop on any level of subfolders & it will work.
The problem
After generating your application with:
$ npx -p @angular/cli ng new my-app
when you build your app with:
$ npm run build
you will get the dist/my-app/index.html file with:
Although src="runtime... looks OK, and it seems it should be working in the browser, when we visit localhost/my-app/dist/my-app, we see nothing else but a white screen. That's because there is:
<basehref="/">
Which sets the context for all location references to the absolute location on our current domain. If you change it manually to <base href="./">, all should work fine - now the only thing is to set it in a way that will not require manual tweaks after every build.
The solution
There are 2 possible solutions - use the CLI parameter, or set the value in the angular.json file.
CLI parameter
angular build accepts --base-href param that allows us to override this value. Adding it manually every time would be a lot of a hassle, so the best to set it in the package.json:
I cannot find it in the official documentation, but it's working & it's recognized by angular.json validation.
Summary
In this article, we have seen how to set up Angular CLI generate app to work from a subfolder. With one or the other solution in place, your built app should look like: