title: Yarn 2 monorepo with babel fails to build because of missing dependences, but it's looking at the wrong package.json
body: <p>I have a monorepo, using yarn 2.4, webpack and babel and a top level <code>babel.config.json</code>, with the following folder structure (slightly simplified)</p>
  <pre><code>|-- .yarn
  |-- package.json (top-level)
  |-- babel.config.json
  |-- tsconfig.json
  |-- /* other config files... /
  |-- modules
  |   |-- build
  |   |   |-- build-system
  |   |   |   |-- package.json (module-level)
  |   |   |   |-- entry.js
  |   |   |   |-- webpack.js
  |   |   |   |-- /* source... */
  |   |-- /* other module folders... */
  </code></pre>
  <p>I've changed my webpack rule for ts/js files to the following:</p>
  <pre><code>{
    test: /\.(j|t)sx?$/,
    use: { 
      loader: &quot;babel-loader&quot;,
      options: {
        babelrcRoots: ['../..'],
        rootMode: 'upward',
      },
    }
  }
  </code></pre>
  <p>Top-level package.json</p>
  <pre><code>{
    &quot;name&quot;: &quot;workspace-example&quot;,
    &quot;version&quot;: &quot;0.0.1&quot;,
    &quot;workspaces&quot;: [
      &quot;modules/build/*&quot;
    ]
  }
  </code></pre>
  <p>Module-level package.json</p>
  <pre><code>{
      &quot;name&quot;: &quot;@workspace-example/build-system&quot;,
      &quot;version&quot;: &quot;0.0.1&quot;,
      &quot;dependencies&quot;: {
          &quot;@babel/core&quot;: &quot;^7.14.5&quot;,
          &quot;@babel/plugin-transform-runtime&quot;: &quot;^7.14.5&quot;,
          &quot;@babel/preset-env&quot;: &quot;^7.14.5&quot;,
          &quot;@babel/preset-react&quot;: &quot;^7.14.5&quot;,
          &quot;@babel/preset-typescript&quot;: &quot;^7.14.5&quot;,
          &quot;@babel/runtime-corejs3&quot;: &quot;^7.14.5&quot;,
          &quot;babel-loader&quot;: &quot;^8.2.2&quot;,
          &quot;html-loader&quot;: &quot;^2.1.2&quot;,
          &quot;pnp-webpack-plugin&quot;: &quot;^1.6.4&quot;,
          &quot;react&quot;: &quot;^17.0.2&quot;,
          &quot;react-dom&quot;: &quot;^17.0.2&quot;,
          &quot;typescript&quot;: &quot;^4.3.2&quot;,
          &quot;webpack&quot;: &quot;^5.38.1&quot;,
          &quot;webpack-cli&quot;: &quot;^4.7.2&quot;,
          &quot;webpack-dev-server&quot;: &quot;^3.11.2&quot;
      },
      &quot;scripts&quot;: {
          &quot;build&quot;: &quot;webpack --config source/webpack/webpack.js&quot;
      }
  }
  </code></pre>
  <p>This seems to correctly make it load the top-level <code>babel.config.json</code>, however now when running a build it's looking in the top-level package.json for dependencies, when they're at the module-level package.json, so it fails with the following error:</p>
  <pre><code>Module build failed (from ../../../.yarn/$$virtual/babel-loader-virtual-7dc78877af/0/cache/babel-loader-npm-8.2.2-b4e600c2c5-362bb71573.zip/node_modules/babel-loader/lib/index.js):
  Error: Your application tried to access @babel/plugin-transform-runtime, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.
  
  Required package: @babel/plugin-transform-runtime (via &quot;@babel\plugin-transform-runtime&quot;)
  Required by: C:\code\webpack-5-yarn\
  </code></pre>
  <p>These dependenceies are defined in the module-level package.json but it is looking at the top-level package.json when I don't believe it should be.</p>
  <p>Moving the top-level <code>babel.config.json</code> to <code>.babelrc</code> in the build-system module and removing the two options from the babel loader rule works (for some reason keeping the name <code>babel.config.json</code> doesn't work). But I'm looking for a solution which can use a single top-level babel.config.json.</p>
  <p>I have the same solution working with webpack 4 and yarn 1 without issue.</p>
  <p><strong>Edit:</strong>
  Upon using .babelrc in the module directory, I can remove all dependencies from the module-level package.json and not get the error <code>Error: Your application tried to access @babel/plugin-transform-runtime, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.</code></p>
  <p>Not sure why I wouldn't be when I think it should be the same issue.</p>
  <p><strong>Edit 2:</strong>
  I  seem to have it partially working by removing the babel.config.json file completely and just adding the configuration directly into the webpack file like so:</p>
  <pre><code>{
    test: /\.(j|t)sx?$/,
    exclude: /node_modules/,
    use: { 
      loader: &quot;babel-loader&quot;,
      options: {
        root,
        presets: [
          &quot;@babel/preset-env&quot;,
          &quot;@babel/preset-react&quot;,
          &quot;@babel/preset-typescript&quot;
        ],
        plugins: [
          [
            &quot;@babel/plugin-transform-runtime&quot;,
            {
              regenerator: true,
              corejs: 3
            }
          ]
        ]
      },
    }
  }
  </code></pre>