angulardependenciesdayjs

Upgrading Angular 18 to 19 has caused ESM warnings for dayjs


I've upgraded to Angular 19 and when I build the project I am getting a few new warnings. One of which is related to dayjs:

▲ [WARNING] Module 'dayjs' used by 'projects/portal/src/app/applets/events/events.component.ts' is not ESM

  CommonJS or AMD dependencies can cause optimization bailouts.
  For more information see: https://angular.dev/tools/cli/build#configuring-commonjs-dependencies


▲ [WARNING] Module 'dayjs/plugin/utc' used by 'projects/portal/src/app/applets/events/events.component.ts' is not ESM

  CommonJS or AMD dependencies can cause optimization bailouts.
  For more information see: https://angular.dev/tools/cli/build#configuring-commonjs-dependencies

Here's my code that's importing dayjs in the file:

import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

dayjs.extend(utc);

My dayjs version in the package.json is "^1.11.10"

I should mention that the project builds and works fine. But I would like to still deal with the warning.


Solution

  • OK I figured it out. Dayjs needs to be added to the build options in angular.json:

    "build": {
       "options": {
          "allowedCommonJsDependencies": [
            "dayjs"
          ]
       }
    }