title: Yarn workspace with webpack: "Module not found: Error: Can't resolve <package>"
body: <p>I am using yarn workspace. I have my root node_modules, then some apps..
  For one of the apps, I use webpack to build it. I need webpack to ignore this root &quot;node_modules&quot; folder.</p>
  <p>However, that's just not happening.</p>
  <p>This is my webpack config file:</p>
  <pre><code>const path = require('path');
  
  const nodeExternals = require('webpack-node-externals');
  const webpack = require('webpack');
  
  module.exports = {
      resolve: {
          extensions: ['.ts', '.js'],
      },
      devtool: 'source-map',
      entry: path.resolve(__dirname, 'src/main.ts'),
      target: 'electron-main',
      externals: [
          nodeExternals({
              allowlist: ['webpack/hot/poll?1'],
              //modulesFromFile: true,
          }),
      ],
      plugins: [
          //new webpack.HotModuleReplacementPlugin(),
          new webpack.NormalModuleReplacementPlugin(/.\[BUNDLE_OS_TARGET\]./, (resource) =&gt; {
              resource.request = resource.request.replace(/\[BUNDLE_OS_TARGET\]/, process.env.OS_TARGET || 'darwin');
          }),
      ],
      module: {
          rules: [
              {
                  test: /\.ts$/,
                  use: 'ts-loader',
                  exclude: /node_modules/,
              },
          ],
      },
      node: {
          __dirname: false,
      },
      output: {
          path: path.resolve(__dirname, 'dist'),
          filename: '[name].js',
      },
  };
  </code></pre>
  <p>When I try to compile the app, I get:</p>
  <pre><code>ERROR in ../../node_modules/@nestjs/microservices/server/server-rmq.js 31:87-121
  Module not found: Error: Can't resolve 'amqp-connection-manager' in 'C:\Users\Tal\Desktop\copyHC\node_modules\@nestjs\microservices\server'
   @ ../../node_modules/@nestjs/microservices/server/index.js 10:21-44
   @ ../../node_modules/@nestjs/microservices/index.js 21:21-40
   @ ./src/main.ts 16:24-56
  </code></pre>
  <p>And when I building the application <strong>without</strong> yarn workspaces (so the node_modules is in the same level as webpack.config.js), but as single repository, It successfully compiles.</p>
  <p>So basically webpack does not ignore it.</p>
  <p>I also did: <code>&quot;exclude&quot;: [&quot;./dist&quot;, &quot;../../node_modules&quot;],</code> in my tsconfig.json file (the relative path points to the root node_modules).</p>
  <p>Then I tried adding this to externals in webpack config:</p>
  <pre><code>nodeExternals({
      modulesDir: path.resolve(__dirname, '../../node_modules'),
  }),
  </code></pre>
  <p>It compiled, but I got the error:
  <a href="https://i.stack.imgur.com/fDyKr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fDyKr.png" alt="enter image description here" /></a></p>