Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Cannot find module" in yarn monorepo setup #472

Closed
epicbytes opened this issue Oct 10, 2022 · 10 comments
Closed

"Cannot find module" in yarn monorepo setup #472

epicbytes opened this issue Oct 10, 2022 · 10 comments
Labels
bug Something isn't working

Comments

@epicbytes
Copy link

Version

5.14.0

Describe the bug

Hi! I ran into a problem when using turborepo, if the module is added to one application, then everything works as it should. But when added to the second, the module starts to give an error when both are started.

Applications are written in NextJS.

Both times the module is connected and initialized according to the instructions.

Reproduction

install module
auto setup module
connected this to one more application in turborepo

Logs

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Volumes/WORK/website/apps/admin/node_modules/typesafe-i18n/react' imported from /Volumes/WORK/website/apps/admin/.next/server/pages/_app.js
Did you mean to import typesafe-i18n/react/index.cjs?
    at new NodeError (node:internal/errors:387:5)
    at finalizeResolution (node:internal/modules/esm/resolve:404:11)
    at moduleResolve (node:internal/modules/esm/resolve:965:10)
    at defaultResolve (node:internal/modules/esm/resolve:1173:11)
    at nextResolve (node:internal/modules/esm/loader:173:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:852:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:439:18)
    at ESMLoader.import (node:internal/modules/esm/loader:536:22)
    at importModuleDynamically (node:internal/modules/cjs/loader:1059:29)
    at importModuleDynamicallyWrapper (node:internal/vm/module:438:21) {
  code: 'ERR_MODULE_NOT_FOUND',
  page: '/'
}

Config

{
   "adapter": "react",
   "$schema": "https://unpkg.com/typesafe-i18n@5.14.0/schema/typesafe-i18n.json",
   "baseLocale": "en"
}

Additional information

No response

@epicbytes epicbytes added the bug Something isn't working label Oct 10, 2022
@ivanhofer
Copy link
Owner

Hi @epicbytes can you share the repository?

@epicbytes
Copy link
Author

https://github.com/epicbytes/sample-bug

This is a monorepo recreated from scratch.
cd /apps/web and run yarn dev, then try to open it in browser

@ivanhofer
Copy link
Owner

I tried the example in StackBlitz and it works when adding a default value on this line

https://github.com/epicbytes/sample-bug/blob/main/apps/web/src/pages/index.tsx#L17

-const locale = context.locale as Locales;
+const locale = context.locale as Locales || baseLocale;

@epicbytes
Copy link
Author

Unfortunately, this does not solve the problem, the error, as I understand it, is that the module does not find itself in the node_modules folder, which is located next to the application, because turborepo has its own shared directory at the root level of the monorepository. at the same time, typesafe tries to write exactly in node_modules next to the project, but does not see its code in it.

The problem arises precisely in the fact that the module is confused among the directories.

@ivanhofer
Copy link
Owner

I was now finally able to reproduce your issue.
I don't think this has anything to do with turbo.
It seems like an issue with yarn and how it installs dependencies in a monorepo setup. I haven't looked in detail but it seems that all dependencies are installed at the root node_modules folder. Therefore Next.js is not able to pick up the imports correctly.
I still wonder why other packages can be resolved correctly, but typesafe-i18n not...

Workaround for now: setting the generateOnlyTypes-option to true and manually prefix all typesafe-i18n imports with ../../../../node_modules to the the library where it is actually located.

@ivanhofer
Copy link
Owner

I have found this issue. It seems the issue is the "hoisting" of packages in yarn.
Changing the workspace config in package.json will fix the issue:

-  "workspaces": [
-    "apps/*",
-    "packages/*"
-  ],
+  "workspaces": {
+    "packages": [
+      "apps/*",
+      "packages/*"
+    ],
+    "nohoist": [
+      "**/typesafe-i18n",
+      "**/typesafe-i18n/**"
+    ]
+  },

@epicbytes
Copy link
Author

Thanks, now it works as it should, it would be great to describe it in the documentation.

@ivanhofer ivanhofer changed the title Turborepo error "Cannot find module" in yarn monorepo setup Oct 14, 2022
@ivanhofer
Copy link
Owner

ivanhofer commented Oct 14, 2022

I'll add a section to the FAQs.

edit: added
https://github.com/ivanhofer/typesafe-i18n#cannot-find-module-in-yarn-monorepo-setup

@ivanhofer
Copy link
Owner

@tinwritescode
Copy link

@ivanhofer I found the same issue happens on Nx monorepo. Is there anyone have solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants