Solvedangular cli How would you exclude moment.js locale in a proper way?

Bug Report or Feature Request

  • [x ] question


@angular/cli: 1.0.1
node: 7.8.0
os: win32 x64


I've got the whole locale ended up in my bundle:


As I am not using --eject flag this solution moment/moment#2416 (comment) in not working for me.

I discovered this SO answer which is kind of not really works for me, getting like Error: Can't resolve './locale' in runtime.

Is there any proper solution with angular-cli to exclude the moment locale without --eject?

39 Answers

✔️Accepted Answer

There is a simplier solution if you want to exlude all locales, in src/ add this :

"compilerOptions": {
    "paths": {
      "moment": [

It will instruct the compiler to use the minified-without-locale bundle instead of building from the sources.

Why use this instead of moment-mini ?

  1. Well the moment-min repo is not always up to date with the official moment repo, therefore you can be stuck on an old version
  2. If you use other libraries that depend on moment (such as moment-timezone), by default it will bundle the full moment with locale into your app. The config above avoid this pitfall.

Other Answers:

I found a solution in momentjs issues, then I tried it with @angular-builders/custom-webpack.
This is my config:


'use strict';

const webpack = require('webpack');

module.exports = {
  plugins: [new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /zh-cn/)]

and angular.json

 "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./extra-webpack.config.js",
              "replaceDuplicatePlugins": true,
              "mergeStrategies": {
                "externals": "prepend"

It works fine. You can select whatever locale you want.

Hope this will help you.

@ciesielskico is your bundle looking good after all?

By importing through the "scripts": [] and then doing import * as moment from 'moment'; just got multiple moments in a bundles - one in the vendor bundle with all locales(same as I initially got), and another one in a scripts bundle.

Anyway it is sounds like there is too much hassle of using current moment until the proper modular version will be released So currently just ended up of using which is works fine for me:

import * as moment from 'moment-mini';


we are using a npm script after install, found somewhere ...

var globby = require('globby');
var rimraf = require('rimraf');

globby(['./node_modules/moment/locale/*', '!./node_modules/moment/locale/de.js', '!./node_modules/moment/locale/de-at.js', '!./node_modules/moment/locale/de-ch.js'])
.then(function then(paths) { map(item) {

Just include the .min.js version which does not include any locales, and then include the locale you need.
This is what I've got:

"scripts": [

Edit: Found out that my solution does not work, and in fact also includes all locales

More Issues: