types-ddd
types-ddd copied to clipboard
Bug trying to implement an ObjectValue in NextJs
Describe the bug
When i try to create a an ObjectValue it throw an error.
Module not found: Can't resolve 'fs'
To Reproduce ObjectValue
import { IResult, Result, ValueObject } from 'types-ddd';
export interface NameProps {
value: string;
}
export class Name extends ValueObject<NameProps> {
private constructor(props: NameProps) {
super(props);
}
public static create(props: NameProps): IResult<Name> {
return Result.Ok(new Name(props));
}
}
export default Name;
const Home: NextPage = () => {
Name.create({value : 'tamon'})
return (
<div className={styles.container}>
</div>
);
};
Error Thrown

hey @tamon-persol thanks for reporting this. This is not a bug of the "types-ddd" lib, this is a limitation of the way next.js generates a package for the browser, but you can change your settings.
try putting this in your package.json
"browser": {
"fs": false,
"path": false,
"os": false,
"child_process": false
}
The "types-ddd" lib uses some dependencies that might not be available to the browser if webpack removes them. Also check your webpack configuration if you are not removing packages used by "types-ddd" like:
- bcrypt
- pino
- pino-pretty
- rich-domain
Alternative: you can use "rich-domain" which is free from 3rd party dependency
Still stuck on this.
I'm using [email protected] I'm calling my api from a route handler.
I've tried:
- adding contents to package.json
- migrating to "rich-domain"
👀
Thanks for coming back so quickly.
Here's my demo repo: https://github.com/HTCH-app/fsd-ddd
Project builds, but pressing the only button on the page makes webpack freak out regarding fs etc.
My project is now working (see repo) with the following setup:
- Nextjs edge router
- Replace
types-ddddep withrich-domain - Addition to package.json:
"browser": {
"crypto": false
},
Perfect @mmmoli,
The types-ddd library uses some dependencies like:
- bcrypt
- pino
- pino-pretty
- rich-domain (core) So the build removes them; however, rich-domain uses only native cryptography (crypto) to generate UUID. In case the crypto module is not available, a native implementation will be used to ensure UUID generation.
I checked, and we don't have a bug, right?
Confirm: no bug!