Angular中ng generate universal与ng add @nguniversal/express-engine的区别
Difference Between
ng add @nguniversal/express-engine and ng generate universal Great question—let’s break down these two Angular Universal commands clearly, since it’s easy to mix them up when getting started with server-side rendering:
ng add @nguniversal/express-engine
- This is the modern, official recommended approach for setting up Angular Universal. It’s a "one-stop shop" command that handles the entire setup process automatically.
- What it does:
- Installs all required dependencies (like
@nguniversal/express-engine,@nguniversal/builders, etc.) - Generates the core Universal code files (yes, it actually calls
ng generate universalunder the hood!) - Updates your
angular.jsonwith SSR-specific build configurations - Adds essential files like
server.ts(the Express server entry point) - Sets up npm scripts in
package.jsonfor running SSR locally (dev:ssr) and building for production (build:ssr,serve:ssr)
- Installs all required dependencies (like
- Perfect for most users, especially beginners, because it eliminates manual configuration and reduces the chance of setup errors.
ng generate universal
- This is a lower-level, more granular command that only handles generating the base Universal code files (like
app.server.module.ts, the server module, etc.). - What it doesn’t do:
- It won’t install any dependencies for you—you’d have to manually add packages like
@nguniversal/express-engineyourself - It won’t update your project’s build configuration or add any npm scripts
- It won’t install any dependencies for you—you’d have to manually add packages like
- You’d only use this if you need full control over the setup process (e.g., customizing dependencies, integrating with a non-Express server) or if you’ve already installed the required packages separately.
To confirm your guess: You’re exactly right—ng add @nguniversal/express-engine includes the functionality of ng generate universal as part of its full setup workflow, but it extends it with all the necessary configuration and dependency management to get SSR up and running quickly.
内容的提问来源于stack exchange,提问作者Mizok.H
相关产品推荐
相关产品推荐

