Skip to content

Commit a26f6b6

Browse files
committed
fix(@angular/build): show error when Node.js built-ins are used during ng serve
This commit ensures consistent behavior between `ng build` and `ng serve`. Previously, `ng serve` did not display an error message when Node.js built-in modules were included in browser bundles. By default, Vite replaces Node.js built-ins with empty modules, which can lead to unexpected runtime issues. This update addresses the problem by surfacing clear error messages, providing better developer feedback and alignment between the two commands. Closes: #27425
1 parent 75998eb commit a26f6b6

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

packages/angular/build/src/builders/dev-server/vite-server.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,25 @@ function getDepOptimizationConfig({
803803
thirdPartySourcemaps: boolean;
804804
}): DepOptimizationConfig {
805805
const plugins: ViteEsBuildPlugin[] = [
806+
{
807+
name: 'angular-browser-node-built-in',
808+
setup(build) {
809+
// This namespace is configured by vite.
810+
build.onLoad({ filter: /.*/, namespace: 'browser-external' }, (args) => {
811+
if (!isBuiltin(args.path)) {
812+
return;
813+
}
814+
815+
return {
816+
errors: [
817+
{
818+
text: `The package "${args.path}" wasn't found on the file system but is built into node.`,
819+
},
820+
],
821+
};
822+
});
823+
},
824+
},
806825
{
807826
name: `angular-vite-optimize-deps${ssr ? '-ssr' : ''}${
808827
thirdPartySourcemaps ? '-vendor-sourcemap' : ''
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import assert from 'node:assert';
2+
import { execAndWaitForOutputToMatch, ng } from '../../utils/process';
3+
import { installPackage } from '../../utils/packages';
4+
import { writeFile } from '../../utils/fs';
5+
6+
export default async function () {
7+
await ng('cache', 'clean');
8+
await ng('cache', 'on');
9+
10+
await installPackage('express@4');
11+
await writeFile('src/main.ts', `import 'express';`);
12+
13+
const { stderr } = await execAndWaitForOutputToMatch('ng', ['serve', '--port=0'], /ERROR/, {
14+
CI: '0',
15+
NO_COLOR: 'true',
16+
});
17+
18+
assert.match(stderr, /The package "http" wasn't found on the file system but is built into node/);
19+
}

0 commit comments

Comments
 (0)