I have an Angular app which runs perfectly in my local and production environment.. After a tiny change I made, I ran the app locally and it works fine.. Then I built the pr
In my case was a mismatch route path in the server file. the route to statics must be the dir containing both the index.html and the assets dir, not the assets dir itself.
app.use(express.static(path.resolve(__dirname,'/dist')));
app.get('/*', function (req, res) {
res.sendFile(path.join(path.resolve(__dirname, '/dist/index.html')));
});
This is most likely the result of a 404 page or a redirect to a page that serves regular html instead of the expected JavaScript files.
(A HTML page starts with <html>
or a <!DOCTYPE...>
)
Make sure that you have correctly uploaded the files and access the page correctly. You can verify by manually accessing the URL with the browser or look into the network tab of your browser development tools to inspect the response.
For a quick workaround though (WHICH IS DEFINITELY NOT RECOMMENDED AND NOT A GOOD PRACTICE) you can build in non prod mode. Try ng build
only.
I also had the same problem a couple of times, I solved it this way:
Build the project with a user other than root (ubuntu - normaluser)
root$ chown -R ubuntu:ubuntu my-project-folder/
ubuntu$ cd my-project-folder/
ubuntu$ npm install
ubuntu$ ng build -xxx params xx etc
In production server show nginx user :
root$ grep -rh user /etc/nginx/*
user www-data; # in my case
Now move or replace my-project-folder
root$ mv my-project-folder/ /var/www/awesome.com
And change permissions to nginx user
root$ chown -R www-data:www-data /var/www/awesome.com
(Y)
in your index.ts
change
<!--doctype html-->
<!DOCTYPE html>
remember... The <!DOCTYPE> declaration is NOT case sensitive. check the following link
The problem is with the <base href="/">
part of index.html
file, it seems angular generates build file inside dist/{yourProjectName}/
but the index.html
files goes through the dist/
for build files. Now you have 2 options:
Change the <base href="/">
part of the index.html
file to <base href="/dist/{yourProjectName}/">
but now you have inconsistency between the ng serve
command and ng build
and you can't see your project through ng serve
. So you have change that part every time!
The seccond approach that I recommend, is just changing the output path of the project!
Go to your angular.json
file of your project and change the "outputPath": "dist/{yourProjcetName}",
to "outputPath": "dist/"
and don't change the base href
!