question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Can't find module` when using monorepo / yarn workspaces

See original GitHub issue

I’m trying to setup yarn workspaces with my docker instance. This is my directory structure:

/monorepo/
  /node_modules/
        @libs/common
        @services/common
        @services/project-A
        ...OTHER DEPS...
  package.json
  /services/
      /common/
         index.jsx
         package.json
      /project-A/  
           webpack.base.config.js
           **REACT project with babel, webpack, etc**
 
  /libs/
     /tools/
         /common/
            index.jsx
            package.json

To simplify my docker setup I just configured this volume within my docker compose that maps the entire monorepo directory:

volumes:
      - '../../../monorepo:/monorepo'

From there in my Project-A I import @libs/common and @services/common. This works fine when the common libraries are exporting simple functions like:

export const Add = (a,b) => a+b

Webpack has no issue resolving this and building Project-A.

However when I try to import a component from one of the common libraries like this:

/libs/tools/common: 

      import React from 'react' 
      export MySharedComponent = () => <>HELLLO</>

I get an error in the build process:

 Error: Cannot find module '/monorepo/libs/tools/common/webpack.base.config.js'
Require stack:
- /monorepo/node_modules/eslint-import-resolver-webpack/index.js
- /monorepo/node_modules/eslint-module-utils/resolve.js
- /monorepo/node_modules/eslint-plugin-import/lib/rules/no-unresolved.js
- /monorepo/node_modules/eslint-plugin-import/lib/index.js

The eslint file under Project-A:

{
    "parser": "babel-eslint",
    "env": {
      "browser": true,
      "node": true,
      "jest": true,
      "cypress/globals": true
    },
    "settings": {
      "import/resolver": {
        "webpack": {
          "config": "webpack.base.config.js"
        }
      }
    }
  }

The babel.rc under Project-A

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ],
    "@babel/preset-react",
    "@babel/preset-flow"
  ],
  "env": {
    "test": {
      "plugins": [
        [
          "babel-plugin-webpack-alias",
          {
            "config": "./webpack.base.config.js"
          }
        ]
      ]
    }
  }
}

My Question:

  • Is the main issue that there’s no webpack config set up in the common repositories. Therefore the workspace does not know how to compile my shared resources?

  • Should there only be 1 webpack build config in my workspace used by all projects within the workspace? Currently I only have 1 config under Project-A?

  • What happens if I have specific webpack needs per project, does 1 config (if that’s the answer) make sense?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
simkessycommented, Mar 5, 2020

Yea I’m realizing now that it’s babel related and likely with my set up and not directly related to this plugin. Just not entirely sure how to fix it yet. Feel free to close if not relevant anymore.

1reaction
dirkpostmacommented, Oct 29, 2022

Not sure if it helps in this particular case, but I solved a similar issue by adding "types": property to the package.json of the dependency. Similar to the "main" prop, but for types.

Example:

"types": "src/types.d.ts",

Reference: https://stackoverflow.com/questions/66081171/lerna-yarn-and-typescript-cannot-find-module-or-its-corresponding-type-declar

Read more comments on GitHub >

github_iconTop Results From Across the Web

`Can't find module` when using monorepo / yarn workspaces
I'm trying to setup yarn workspaces with my docker instance. This is my directory structure: /monorepo/ /node_modules/ @libs/common ...
Read more >
Monorepo – Yarn workspaces Typescript Node.JS project
Monorepo – Yarn workspaces Typescript Node.JS project – cannot find module when running nodemon ; Root package.json · "name" · "@bhirmer/monorepo" ...
Read more >
TypeScript Monorepos with Yarn - Semaphore CI
A tutorial showing how to configure TypeScript to work in a monorepo and building everything with CI/CD.
Read more >
Q: Using yarn workspace but Typescript isn't aware of it - Reddit
When I import something from an another workspace I get Cannot find module @org/common/Movie or its corresponding type... from the TS…
Read more >
Jest monorepo cannot find module
Jest is failing with error: Cannot find module ... This time, we'll build a modular React Native app using a Yarn Workspaces monorepo,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found