types-ddd icon indicating copy to clipboard operation
types-ddd copied to clipboard

Bug trying to implement an ObjectValue in NextJs

Open tamon-persol opened this issue 3 years ago • 1 comments

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 Screen Shot 2022-09-14 at 6 05 32

tamon-persol avatar Sep 13 '22 21:09 tamon-persol

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

4lessandrodev avatar Sep 14 '22 15:09 4lessandrodev

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"

mmmoli avatar Dec 14 '23 13:12 mmmoli

Can you send some example?

Check this: Sample

4lessandrodev avatar Dec 14 '23 14:12 4lessandrodev

👀

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.

mmmoli avatar Dec 14 '23 14:12 mmmoli

My project is now working (see repo) with the following setup:

  1. Nextjs edge router
  2. Replace types-ddd dep with rich-domain
  3. Addition to package.json:
   "browser": {
    "crypto": false
   },

mmmoli avatar Dec 15 '23 07:12 mmmoli

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?

image image

4lessandrodev avatar Dec 15 '23 11:12 4lessandrodev

Confirm: no bug!

mmmoli avatar Dec 15 '23 13:12 mmmoli