It turned out that my organization's package setup was causing a lot of the problems. Specifically, some subpackages were tightly coupled with others, and during the build process, peerDependencies (like next) were being installed unexpectedly, leading to issues.
What helped me was switching to "workspace:*" in the package.json of my subpackages. This made dependency resolution local to the workspace and significantly improved things. For example:
"dependencies": {
"some-subpackage": "workspace:*"
}
This approach reduces coupling and ensures the local packages are used during builds. However, I'm still facing some ERR_MODULE_NOT_FOUND issues, which seem to be related to module resolution or mixed module types (CommonJS vs ESM). It's not the full solution, but switching to "workspace:*" might help others facing similar problems!"