Search code examples
angularnpmbuildproduction

Building Angular for production without installing devDependencies


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?


Solution

  • 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