Angular Universal技术疑问:服务器需装npm包吗?构建包含依赖吗?
Let me break this down clearly for you based on how Angular Universal works:
1. Does main.bundle.js include npm dependency code?
Yes! The client-side bundle (like main.bundle.js in your dist/browser folder) behaves exactly like a regular Angular app's bundle. It includes all your custom code plus the compiled versions of npm dependencies (Angular core, moment, etc.) that your client-side code needs. This is because the client bundle is meant to run in the browser, which can't access your server's node_modules folder.
A quick note to avoid confusion: The server-side bundle (like main.server.js referenced by server.js) is different—it doesn’t bundle npm dependencies. Instead, it keeps references to them since Node.js can directly resolve modules from node_modules when running the server. But your question specifically asks about main.bundle.js, so for that client bundle, all required dependencies are packaged inside.
2. Do you need to install npm packages on the server?
This depends entirely on what you’re deploying:
- If you’re only serving pre-rendered static files: No need. The pre-rendered HTML and client bundles already contain everything the browser needs to run your app. You can host the
dist/browserfolder with any static file server (Nginx, Apache, etc.) without installing any npm packages. - If you’re running the Universal SSR server (using
server.js): Yes, you must install all production dependencies on the server. Since the server-side bundle doesn’t packagenode_modulescode, Node.js needs those packages present to resolve imports when starting the server. Just runnpm install --productionin your project directory on the server (make sure you’ve copied over yourpackage.jsonandpackage-lock.jsonfirst).
内容的提问来源于stack exchange,提问作者user1879408

