title: Bundle npm package with webpack but exclude all vendor packages
body: <p>I've spent a couple of hours already on this problem and I'm stuck.</p>
  
  <p><strong>tl;dr:</strong> Bundling my custom component with webpack leads in either an <code>undefined</code> or <code>'call' of undefined</code> error (when loading unbundled vendor packages). See generated webpack config below.</p>
  
  <hr>
  
  <p>I created a <strong>mono repo with a simple TypeScript package</strong>. Everything works fine when I just transpile it with tsc. But as soon as I use <strong>Relay</strong>, I run into <strong>problems</strong>. So I figured, I just <strong>use webpack</strong>. Easier said than done.</p>
  
  <p>Of course, I only want to bundle, what is necessary. And there is my current problem: I get an error when the bundled package tries to resolve <code>../../node_modules/react/index.js</code>.</p>
  
  <p>I think <code>commonjs2</code> might be the way to go as I am using that package within another webpack project.</p>
  
  <pre><code>const webpackConfig = {
    mode: "production",
    resolve: {
      extensions: [ ".web.js", ".mjs", ".js", ".json", ".web.jsx", ".jsx", ".tsx", ".ts" ],
      modules: [
        "&lt;mono-repo-root&gt;/node_modules",
        "&lt;mono-repo-root&gt;/packages/custom-navigation/packages"
      ],
      alias: {
        packages: "&lt;mono-repo-root&gt;/packages/custom-navigation/packages",
        react: "&lt;mono-repo-root&gt;/node_modules/react",
        "react-dom": "&lt;mono-repo-root&gt;/node_modules/react-dom"
      }
    },
    module: {
      rules: [
        {
          test: /\.ts(x)?$/,
          enforce: "pre",
          use: [
            {
              loader: "tslint-loader",
              options: {
                configFile: "&lt;mono-repo-root&gt;/tslint.js",
                tsConfigFile: "&lt;mono-repo-root&gt;/tsconfig.json",
                formatter: "stylish",
                emitErrors: false
              }
            }
          ]
        },
        {
          test: /\.ts(x)?$/,
          exclude: [ /node_modules/ ],
          use: [
            {
              loader: "babel-loader",
              options: {
                presets: [ "@babel/preset-react" ],
                plugins: [ "relay" ]
              }
            },
            {
              loader: "ts-loader",
              options: {
                configFile: "&lt;mono-repo-root&gt;/packages/custom-navigation/tsconfig.json"
              }
            }
          ]
        },
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: "javascript/auto"
        }
      ]
    },
    plugins: [
      // Add module names to factory functions so they appear in browser profiler.
      new webpack.NamedModulesPlugin(),
      // Makes some environment variables available to the JS code, for example:
      // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
      new webpack.EnvironmentPlugin( getEnv() ),
      // Watcher doesn't work well if you mistype casing in a path so we use
      // a plugin that prints an error when you attempt to do this.
      // See https://github.com/facebookincubator/create-react-app/issues/240
      new CaseSensitivePathsPlugin(),
      // Moment.js is an extremely popular library that bundles large locale files
      // by default due to how Webpack interprets its code. This is a practical
      // solution that requires the user to opt into importing specific locales.
      // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
      // You can remove this if you don't use Moment.js:
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
    ],
    node: {
      "dgram": "empty",
      "fs": "empty",
      "net": "empty",
      "tls": "empty",
      "child_process": "empty"
    },
    optimization: {
      minimize: false,
      splitChunks: {
        chunks: "all"
      }
    },
    target: "node",
    entry: "&lt;mono-repo-root&gt;/packages/custom-navigation/Navigation.tsx",
    output: {
      path: "&lt;mono-repo-root&gt;/packages/custom-navigation/dist",
      filename: "index.js",
      library: "@custom/navigation",
      libraryTarget: "commonjs2", // tried umd, commonjs and commonjs2
      publicPath: "/dist/"
    },
    externals: [
      webpackNodeExternals(), // npmjs.com/package/webpack-node-externals
      webpackNodeExternals( { modulesDir: 'packages/custom-navigation/node_modules' } )
    ]
  }
  </code></pre>