Search code examples
angularexpressangular-universalangular-ssr

Angular SSR complains about non-ESM modules in my Express API code


I have an Angular SSR project set up to run my Express API as part of the SSR service. All my app code lives in /src/app, and all my API code lives in /src/api. There are some imports of my API code into the app code, but I've tried to limit this to just using the same type definitions that my backend uses (so there's stuff like import type {MyBackendResponse} from '../../../api/types/myBackend.ts').

When I run ng build in my app, it compiles, but with a bunch of warnings that look like:

▲ [Warning] Module 'express-session' used by 'src/api/index.ts' is not ESM

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

I'm confident that none of these modules are being included in the browser code - I've searched all of the dist/<appname>/browser folder, and there's no mention of these libraries, while the server folder does. I'm currently ignoring these warnings, but I'd really like it if Angular actually scolded me about it if these were making their way into my browser code bundles.

Am I actually goofing up my setup somehow, and these warnings are accurate? Or is there a way to make Angular not complain about these files? (Note: I do NOT want to just turn off the warnings for these packages - I want Angular to yell at me if these are included in the browser bundles! I just don't care if it's included in the server-side code.)


Solution

  • Fixed it (and another issue at the same time)!

    Turns out that this was caused by Angular trying to bundle these dependencies into its .mjs server output. This can be fixed by adding the libraries to the projects.<appName>.architect.build.options.externalDependencies property in your angular.json. Adding this caused all the warnings to go away, AND as an added bonus, my app actually runs server-side now! (It was previously complaining about a reference to __dirname being undefined.)