Repository navigation
Description
Which @angular/* package(s) are the source of the bug?
Don't known / other
Is this a regression?
No
Description
Hot Module Replacement (HMR) Problems with Module "NodeNext"
Setting "module" to "NodeNext" in tsconfig.json leads to errors during HMR, when changing the template or styles of a component.
This repository is showcasing this behaviour by providing two npm scripts esnext and nodenext.
To understand the problem further you can checkout following explanation:
Example
TS:
@Component({
selector: 'app-root',
template: `<div class='title'>{{title}}</div>`,
styles: '.title { font-size: xxx-large }',
})
export class AppComponent {
title = 'hello world'
}Running npm run esnext will start the dev server with module set to Esnext and moduleResolution set to bundler.
When now for example changing font-size to x-large will trigger an update, the build will be rerun,
a network request to http://localhost:4200/@ng/component?c=src%2Fcomponent.ts%40AppComponent&t=<timestamp will be made,
following code will be loaded and thus replaces the component with the update correctly.
export default function AppComponent_UpdateMetadata(AppComponent, ɵɵnamespaces) {
const ɵhmr0 = ɵɵnamespaces[0];
AppComponent.ɵfac = function AppComponent_Factory(__ngFactoryType__) {
return new (__ngFactoryType__ || AppComponent)();
}
;
AppComponent.ɵcmp = /*@__PURE__*/
ɵhmr0.ɵɵdefineComponent({
type: AppComponent,
selectors: [["app-root"]],
decls: 2,
vars: 1,
consts: [[1, "title"]],
template: function AppComponent_Template(rf, ctx) {
if (rf & 1) {
ɵhmr0.ɵɵelementStart(0, "div", 0);
ɵhmr0.ɵɵtext(1);
ɵhmr0.ɵɵelementEnd();
}
if (rf & 2) {
ɵhmr0.ɵɵadvance();
ɵhmr0.ɵɵtextInterpolate(ctx.title);
}
},
styles: ["\n\n.title[_ngcontent-%COMP%] {\n font-size: x-large;\n}\n/*# sourceMappingURL=component.css.map */"]
});
(() => {
(typeof ngDevMode === "undefined" || ngDevMode) && ɵhmr0.ɵsetClassDebugInfo(AppComponent, {
className: "AppComponent",
filePath: "src/component.ts",
lineNumber: 8
});
})();
}Running npm run nodenext instead will start the dev server with module set to NodeNext.
When doing the same change the network request will instead deliver following code:
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = AppComponent_UpdateMetadata;
function AppComponent_UpdateMetadata(AppComponent, ɵɵnamespaces) {
const ɵhmr0 = ɵɵnamespaces[0];
AppComponent.ɵfac = function AppComponent_Factory(__ngFactoryType__) {
return new (__ngFactoryType__ || AppComponent)();
};
AppComponent.ɵcmp = /*@__PURE__*/
ɵhmr0.ɵɵdefineComponent({
type: AppComponent,
selectors: [["app-root"]],
decls: 2,
vars: 1,
consts: [[1, "title"]],
template: function AppComponent_Template(rf, ctx) {
if (rf & 1) {
ɵhmr0.ɵɵelementStart(0, "div", 0);
ɵhmr0.ɵɵtext(1);
ɵhmr0.ɵɵelementEnd();
}
if (rf & 2) {
ɵhmr0.ɵɵadvance();
ɵhmr0.ɵɵtextInterpolate(ctx.title);
}
},
styles: ["\n\n.title[_ngcontent-%COMP%] {\n font-size: x-large;\n}\n/*# sourceMappingURL=component.css.map */"]
});
(() => {
(typeof ngDevMode === "undefined" || ngDevMode) && ɵhmr0.ɵsetClassDebugInfo(AppComponent, {
className: "AppComponent",
filePath: "src/component.ts",
lineNumber: 8
});
})();
}This code will throw following error:
zone.js:2317 ReferenceError: exports is not defined
at component?c=src%2Fco…=1744143776918:2:23
Please provide a link to a minimal reproduction of the bug
Please provide the exception or error you saw
zone.js:2317 ReferenceError: exports is not defined
at component?c=src%2Fco…=1744143776918:2:23
Please provide the environment you discovered this bug in (run ng version)
% ng version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 19.2.6
Node: 20.15.1
Package Manager: npm 10.9.2
OS: linux x64
Angular: 19.2.5
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1902.6
@angular-devkit/build-angular 19.2.6
@angular-devkit/core 19.2.6
@angular-devkit/schematics 19.2.6
@angular/cli 19.2.6
@schematics/angular 19.2.6
rxjs 7.8.2
typescript 5.8.3
zone.js 0.15.0
Anything else?
Additional problems with Module "NodeNext"
Additionally to the HMR problem the setting also seems to break the angular LSP for templates.
The local LSP of your Editor will load the tsconfig.json so make sure to adjust it accordingly.
Activity
fix(compiler-cli): ensure HMR works with different output module type
alan-agius4 commentedon Apr 9, 2025
Out of curiosity, why are you using
NodeNextin a web application? This typically results in CommonJS output, which isn’t optimized for the web and can unnecessarily inflate your bundle size.DeusProx commentedon Apr 9, 2025
@alan-agius4
In my opinion using
esnextandbundleris the right way to bundle an angular application in general, but a collegue of mine had the task to migrate a lot of projects to ESM and NodeNext was choosen since it supports conditional exports, subpath exports and especially forces to use suffixes (.js) in imports. This improved other parts as this angular project is just included as a webcomponent at other places.We just found out after this switch that it broke HMR and LSP in this part of the project and reverted for now until further feedback.
kayahr commentedon Apr 9, 2025
NodeNexttypically does NOT result in CommonJS output. TypeScript itself generates ESM output when module resolution is set toNodeNextund package.json is set to typemodule.NodeNextmodule resolution is necessary to use dependencies which use modern exports (conditional exports, sub-path exports). They can't be imported by TypeScript when usingESNext.alan-agius4 commentedon Apr 9, 2025
There seems to be some confusion between the
moduleandmoduleResolutionoptions.The
moduleoption defines the output module format, whilemoduleResolutioncontrols how modules are resolved. In most bundled applications, it's recommended to usemodule: preserve, which automatically enablesmoduleResolution: bundler. This setup differs frommodule: NodeNext, which requires explicit file extensions due to the Node.js runtime's behavior.With modern export patterns, setting
moduletoNodeNextisn't strictly necessary. The behavior you're observing where conditional exports work withmodule: NodeNextis actually due to the fact that this setting also switchesmoduleResolutiontoNodeNext. So, it's themoduleResolutionthat drives this functionality, and similar results can be achieved usingmoduleResolution: bundlerormodule: preservewithout the hard requirement to include file extensions.From TypeScript documentation
TL;DR: Unless you are developing an unbundled Node.js application or library, setting
moduletoNodeXXis not ideal, as it conflicts with the intention to create a bundled web app and can lead to CommonJS output in cases where TypeScript does not detect the input module as ESM.fix(compiler-cli): ensure HMR works with different output module type (…
fix(compiler-cli): ensure HMR works with different output module type (…
DeusProx commentedon Apr 10, 2025
Thanks for the input and fast fix everyone!
angular-automatic-lock-bot commentedon May 11, 2025
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.