Building Angular for production without installing devDependencies

10,037

Solution 1

Don't worry about your dependencies.

When you trigger a production build with the @angular/cli via

ng build --prod

Angular will treeshake unused packages and code out of your bundle! See https://github.com/angular/angular-cli/wiki/build for all the build options you can set.

The only thing you have to consider is, how you use your packages and if your dependencies support treeshaking as well. Avoid imports like:

import * as xy from 'packageXY';

Another hint is the npm package Webpack-Bundle-Analyzer Execute these commands in order to inspect/anlayze your bundle:

npm i webpack-bundle-analyzer

ng build --prod --stats-json

cd dist 

webpack-bundle-analyzer stats.json

enter image description here

Solution 2

All the npm packages you use in development phase won't be included in your production build. It does not require any special command when installing your packages. You install all the packages you need or run npm install for an existing project as usual.

When deploying your Angular app with ng build prod it only includes critical packages to your app and dependencies you include in your angular.json file under the build section.

Try it yourself: take any angular project, run npm install or add npm packages then run ng build prod, navigate into the dist folder inside your project folder and look how there is less stuff compared to your project, especially inside node_modules folder.

Share:
10,037
Scuba Kay
Author by

Scuba Kay

Software Engineer working on building an Angular frontend on top of a legacy application!

Updated on June 05, 2022

Comments

  • Scuba Kay
    Scuba Kay almost 2 years

    I am currently trying to create a Docker container to build my production Angular app. I am using npm. I want to install dependencies only (so no devDependencies), so I want to do this:

    npm install --only=prod ng build project-name --prod

    The only problem is that I am missing a lot of packages like @angular/cli and @angular-devkit/build-angular for my build. I couldn't find any good solutions on the internet, but I don't want my build to include all my devDependencies. Also, I don't want my production build to contain any of the packages required to build. Is there a good solution for this?