Angular Universal (SSR) Error: Failed to lookup view "index" in views directory
Meanwhile, I found the solution to deploy on Elastic Beanstalk.
Below the different steps I did:
- In your Elastic Beanstalk Application, edit the
Software Configuration
by changing the Node command bynode dist/server.js
Edit the port used by nodejs (2 options) :
a. You can simply edit the port in your
server.ts
file by 8081 (corresponding to the nodejs port used in the nginx default configuration)const PORT = process.env.PORT || 8081;
b. OR you can edit your nginx configuration with a configuration file .ebextensions to match the port used in your angular universal app (4000 by default)
Instead of:
const DIST_FOLDER = join(process.cwd(), 'dist/browser');
write:
const DIST_FOLDER = join(process.cwd(), 'rootpathofServer/dist/browser');
Example:
const DIST_FOLDER = join(process.cwd(), 'root/www/prod/myProject/dist/browser');
root/www/prod/myProject/dist/browser
The above line refer to the path of my project (build path) from root in nginx aws directory.
And if not working, just console the CWD:
console.log('processPath',process.cwd());
In my case it return / only, so complete path is like /root/www/prod/myProject/dist/browser, which is ('/' + 'root/www/prod/myProject/dist/browser') for this above example.
Yeah it is also true nginx root directory is different from local i.e y it works locally fine and give error on server after deployment.
It took days to sort this issue, I hope it will help you out.
The main problem is simple: it is not able to get index file at particular location or directory.
#LearningNeverEnds
Use the dist
folder as your archive instead of the contents of the dist
folder.
Set the following 'node command' setting in your elastic beanstalk configuration.
node dist/server
This should allow you to run locally and on AWS EB with the standard Angular out of the box setup.
EDIT Just to give a little more info, this should fix the problem. As pointed out by @Kartik Chandra the problem is in the server.ts file on line
const distFolder = join(process.cwd(), 'dist/landing/browser');
When deploying on your prod env process will look for the dist folder, so if you do not copy it it will fail.
Options are to copy the entiry dist folder or changing your server.ts, however this will break your local development.