I am facing an extremely weird behavior with my Angular project. Context:
- I have an NX workspace with angular and nestJS
- They have their own libs but there is also a shared lib for DTOs. Purpose: I defined DTOs once and both front and back end can share these.
Let's say that I have ComponentA and ComponentB. ComponentA also has a service ServiceA.
There is a DTO file in the DTO library for countries: country.dto.ts. This file has DTOs for get/create: CountryGetDto and CountryCreateDto. They typically look like this:
import { IsDateString, IsNotEmpty, IsNumber, IsOptional, IsString } from "class-validator";
import { Country, Continent, City } from "@prisma/client"
import { ApiProperty } from '@nestjs/swagger';
export class CountryCreateDto {
@ApiProperty()
@IsNumber()
@IsOptional()
id?: number;
...
}
export class CountryGetDto {
@ApiProperty()
@IsNumber()
@IsOptional()
id?: number;
...
}
The awkward situation is that I am getting bunch of compilation errors such as:
./node_modules/*/index.js:18:14-37 - Error: Module not found: Error: Can't resolve '' in '/home/user/git/app/node_modules/' BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to: - add a fallback 'resolve.fallback: { "util": require.resolve("util/") }' - install 'util' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "util": false }
ONLY when I import the CountryCreateDto into ComponentA or its children.
I can import CountryGetDto into ComponentA with no problem!
I can import CountryCreateDto into ComponentB with no problem!
I can import CountryCreateDto into ServiceA with no problem!
Now the best part is that I am importing in the NX workspace with @ scopes:
import { CountryGetDto } from '@myapp/dtos'; // works
import { CountryCreateDto } from '@myapp/dtos'; // DOES NOT work
import { CountryCreateDto } from '../../../../../../shared/dtos/src/lib/country/country.dto'; // works
Sadly, I can not reproduce this error in a minimal format. I also don't know if it is the NX or Angular is the problem. I can't interpret the compilation errors, they don't make any sense in this context.
Edit: For instance one of the errors is this:
./node_modules/@nestjs/common/file-stream/streamable-file.js:7:17-34 - Error: Module not found: Error: Can't resolve 'stream' in '/home/user/git/myapp/node_modules/@nestjs/common/file-stream' And here is the relevant line in that file: const stream_1 = require("stream");
New edit: it is definitely @nestjs/swagger, which after being removed solves the compilation issues.
(Note: I read that importing swagger (in this case indirectly via DTOs) can cause this issue. I tried to remove swagger from CountryDto, which did not help. I am importing similarly other DTOs with not issue.)