I have created one website in angular 9 and integrated angular universal using ng add @nguniversal/express-engine
command and it’s integrated successfully.
When I build the application it builds successfully but when I try to run the app using universal it throws error from ** main.js file ** and doesn’t run. Here’s the error:
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev:ssr: `ng run seo-universal:serve-ssr`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev:ssr script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
(screenshot of the error just in case)
-
Command used to build the application:
npm run build:ssr
-
Command used to run the application server side (using angular universal):
npm run dev:ssr
To make website SEO friendly I want to run the application at server side. I have tried almost all the solutions that I found but no luck so far and stuck there.
Thanks in advance.
2
Answers
It’s not because of ng universal, the document is not defined. declare it as below :
From what I can gather from the screenshot it seems you may be using a 3rd party library that uses DOM methods which are not allowed in SSR for Angular since Node.js does not have these methods built in.
You can use the library domino to use DOM on Node.js. Also check out this section in the universal guide: https://angular.io/guide/universal#working-around-the-browser-apis