<h1 dir="auto">Bug report</h1>
<h2 dir="auto">Note</h2>
<p dir="auto">This is a similar issue to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="138133596" data-permission-text="Title is private" data-url="https://github.com/webpack/webpack/issues/2134" data-hovercard-type="issue" data-hovercard-url="/webpack/webpack/issues/2134/hovercard" href="https://github.com/webpack/webpack/issues/2134">#2134</a>, but the context is not exactly the same, so I chose to open another issue.</p>
<h2 dir="auto">What is the current behavior?</h2>
<p dir="auto">Here's the configuration for the server bundle: <a href="https://gist.github.com/tstirrat15/38a1ab2b768406496284d5fdfbef63a6">https://gist.github.com/tstirrat15/38a1ab2b768406496284d5fdfbef63a6</a></p>
<p dir="auto">When I run our production build and inspect the output, I see:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// ...
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(0);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
// ...
/* WEBPACK VAR INJECTION */(function(module) {/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
// ...
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(0);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_2__);
// ..."><pre class="notranslate"><span class="pl-c">// ...</span>
<span class="pl-c">/* harmony import */</span> <span class="pl-k">var</span> <span class="pl-s1">react__WEBPACK_IMPORTED_MODULE_1__</span> <span class="pl-c1">=</span> <span class="pl-en">__webpack_require__</span><span class="pl-kos">(</span><span class="pl-c1">0</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-c">/* harmony import */</span> <span class="pl-k">var</span> <span class="pl-s1">react__WEBPACK_IMPORTED_MODULE_1___default</span> <span class="pl-c1">=</span> <span class="pl-c">/*#__PURE__*/</span><span class="pl-s1">__webpack_require__</span><span class="pl-kos">.</span><span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-s1">react__WEBPACK_IMPORTED_MODULE_1__</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-c">// ...</span>
<span class="pl-c">/* WEBPACK VAR INJECTION */</span><span class="pl-kos">(</span><span class="pl-k">function</span><span class="pl-kos">(</span><span class="pl-s1">module</span><span class="pl-kos">)</span> <span class="pl-kos">{</span><span class="pl-c">/* harmony import */</span> <span class="pl-k">var</span> <span class="pl-s1">react__WEBPACK_IMPORTED_MODULE_0__</span> <span class="pl-c1">=</span> <span class="pl-en">__webpack_require__</span><span class="pl-kos">(</span><span class="pl-c1">0</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-c">/* harmony import */</span> <span class="pl-k">var</span> <span class="pl-s1">react__WEBPACK_IMPORTED_MODULE_0___default</span> <span class="pl-c1">=</span> <span class="pl-c">/*#__PURE__*/</span><span class="pl-s1">__webpack_require__</span><span class="pl-kos">.</span><span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-s1">react__WEBPACK_IMPORTED_MODULE_0__</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-c">// ...</span>
<span class="pl-c">/* harmony import */</span> <span class="pl-k">var</span> <span class="pl-s1">react__WEBPACK_IMPORTED_MODULE_2__</span> <span class="pl-c1">=</span> <span class="pl-en">__webpack_require__</span><span class="pl-kos">(</span><span class="pl-c1">0</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-c">/* harmony import */</span> <span class="pl-k">var</span> <span class="pl-s1">react__WEBPACK_IMPORTED_MODULE_2___default</span> <span class="pl-c1">=</span> <span class="pl-c">/*#__PURE__*/</span><span class="pl-s1">__webpack_require__</span><span class="pl-kos">.</span><span class="pl-en">n</span><span class="pl-kos">(</span><span class="pl-s1">react__WEBPACK_IMPORTED_MODULE_2__</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-c">// ...</span></pre></div>
<p dir="auto">Where <code class="notranslate">__webpack_require__(0)</code> is:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = 868);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

module.exports = require(&quot;react&quot;);

/***/ }),
// ..."><pre class="notranslate"><span class="pl-c">/******/</span> 	<span class="pl-c">// Load entry module and return exports</span>
<span class="pl-c">/******/</span> 	<span class="pl-k">return</span> <span class="pl-en">__webpack_require__</span><span class="pl-kos">(</span><span class="pl-s1">__webpack_require__</span><span class="pl-kos">.</span><span class="pl-c1">s</span> <span class="pl-c1">=</span> <span class="pl-c1">868</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-c">/******/</span> <span class="pl-kos">}</span><span class="pl-kos">)</span>
<span class="pl-c">/************************************************************************/</span>
<span class="pl-c">/******/</span> <span class="pl-kos">(</span><span class="pl-kos">[</span>
<span class="pl-c">/* 0 */</span>
<span class="pl-c">/***/</span> <span class="pl-kos">(</span><span class="pl-k">function</span><span class="pl-kos">(</span><span class="pl-s1">module</span><span class="pl-kos">,</span> <span class="pl-s1">exports</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>

<span class="pl-s1">module</span><span class="pl-kos">.</span><span class="pl-c1">exports</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">"react"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-c">/***/</span> <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
<span class="pl-c">// ...</span></pre></div>
<p dir="auto">This indicates that there are at least 3 instances of React floating around during execution. This hasn't been a problem in the past, but with recent changes to the React context API, React now needs to be a singleton.</p>
<p dir="auto">In <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="138133596" data-permission-text="Title is private" data-url="https://github.com/webpack/webpack/issues/2134" data-hovercard-type="issue" data-hovercard-url="/webpack/webpack/issues/2134/hovercard" href="https://github.com/webpack/webpack/issues/2134">#2134</a>, I found a recommendation to use <code class="notranslate">optimization: { runtimeChunk: 'single' }</code>, and when I tried that, I only found a single instance of React in the bundle, which was good. However, running <code class="notranslate">node build/server.js</code> immediately exited 0 without waiting for requests.</p>
<p dir="auto">Is there something obvious that I'm missing? If so, I'd be happy to add it to documentation.</p>
<h2 dir="auto">If the current behavior is a bug, please provide the steps to reproduce.</h2>
<p dir="auto">I don't have a minimal repro yet. My impression is that it's the result of the confluence of a bunch of different things, rather than an easily localized bug.</p>
<h2 dir="auto">What is the expected behavior?</h2>
<p dir="auto">A single instance of React, or a working server build with <code class="notranslate">runtimeChunk: 'single'</code>.</p>
<h2 dir="auto">Additional context</h2>
<ul dir="auto">
<li>This is an isomorphic React app, with the majority of code being shared between frontend and backend, using two different entrypoints.</li>
<li>We make extensive use of code-splitting</li>
</ul>
<p dir="auto">Please let me know if there's any additional context that would be helpful.</p>
<h2 dir="auto">Other relevant information:</h2>
<p dir="auto">webpack version:  4.29.6<br>
Node.js version: 8.15.1<br>
Operating System: Ubuntu<br>
Additional tools: (like?)</p>
<h2 dir="auto">tl;dr</h2>
<p dir="auto">Webpack should produce the same build whether a module is linked or not.</p>
<h2 dir="auto">In depth</h2>
<p dir="auto">Considering the following dependency trees:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="project@1.0.0
├── babel-runtime@5.1.9
└── sub-project@1.0.0"><pre class="notranslate"><span class="pl-s1">project</span>@<span class="pl-c1">1.0</span><span class="pl-c1">.0</span>
<span class="pl-s1">├──</span> <span class="pl-s1">babel</span><span class="pl-c1">-</span><span class="pl-s1">runtime</span>@<span class="pl-c1">5.1</span><span class="pl-c1">.9</span>
<span class="pl-s1">└──</span> <span class="pl-s1">sub</span><span class="pl-c1">-</span><span class="pl-s1">project</span>@<span class="pl-c1">1.0</span><span class="pl-c1">.0</span></pre></div>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="sub-project@1.0.0
└── babel-runtime@5.1.9"><pre class="notranslate"><span class="pl-s1">sub</span><span class="pl-c1">-</span><span class="pl-s1">project</span>@<span class="pl-c1">1.0</span><span class="pl-c1">.0</span>
<span class="pl-s1">└──</span> <span class="pl-s1">babel</span><span class="pl-c1">-</span><span class="pl-s1">runtime</span>@<span class="pl-c1">5.1</span><span class="pl-c1">.9</span></pre></div>
<p dir="auto"><strong>Notice:</strong> NPM does not include <code class="notranslate">babel-runtime</code> under <code class="notranslate">sub-project</code> when <code class="notranslate">npm list</code> is called from <code class="notranslate">project</code>.</p>
<p dir="auto">Webpack builds something link:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="[0] multi main 52 bytes {0} [built]
[1] ./index.js 2.08 kB {0} [built]
[2] ./~/babel-runtime/helpers/interop-require-wildcard.js 148 bytes {0} [built]
[3] ./~/sub-project/index.js 2.46 kB {0} [built]"><pre class="notranslate"><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-s1">multi</span> <span class="pl-s1">main</span> <span class="pl-c1">52</span> <span class="pl-s1">bytes</span> <span class="pl-kos">{</span><span class="pl-c1">0</span><span class="pl-kos">}</span> <span class="pl-kos">[</span><span class="pl-s1">built</span><span class="pl-kos">]</span>
<span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-kos">.</span><span class="pl-c1">/</span><span class="pl-s1">index</span><span class="pl-kos">.</span><span class="pl-c1">js</span> <span class="pl-c1">2.08</span> <span class="pl-s1">kB</span> <span class="pl-kos">{</span><span class="pl-c1">0</span><span class="pl-kos">}</span> <span class="pl-kos">[</span><span class="pl-s1">built</span><span class="pl-kos">]</span>
<span class="pl-kos">[</span><span class="pl-c1">2</span><span class="pl-kos">]</span> <span class="pl-kos">.</span><span class="pl-c1">/</span><span class="pl-c1">~</span><span class="pl-pds"><span class="pl-c1">/</span>babel-runtime<span class="pl-c1">/</span>helpers</span><span class="pl-c1">/</span><span class="pl-s1">interop</span><span class="pl-c1">-</span><span class="pl-en">require</span><span class="pl-c1">-</span><span class="pl-s1">wildcard</span><span class="pl-kos">.</span><span class="pl-c1">js</span> <span class="pl-c1">148</span> <span class="pl-s1">bytes</span> <span class="pl-kos">{</span><span class="pl-c1">0</span><span class="pl-kos">}</span> <span class="pl-kos">[</span><span class="pl-s1">built</span><span class="pl-kos">]</span>
<span class="pl-kos">[</span><span class="pl-c1">3</span><span class="pl-kos">]</span> <span class="pl-kos">.</span><span class="pl-c1">/</span><span class="pl-c1">~</span><span class="pl-pds"><span class="pl-c1">/</span>sub-project<span class="pl-c1">/</span>index</span><span class="pl-kos">.</span><span class="pl-c1">js</span> <span class="pl-c1">2.46</span> <span class="pl-s1">kB</span> <span class="pl-kos">{</span><span class="pl-c1">0</span><span class="pl-kos">}</span> <span class="pl-kos">[</span><span class="pl-s1">built</span><span class="pl-kos">]</span></pre></div>
<p dir="auto">If we run <code class="notranslate">npm link sub-project</code> we obtain the following tree:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="project@1.0.0
├── babel-runtime@5.1.9
└── sub-project@1.0.0 -&gt; /Users/fc/Development/sub-project"><pre class="notranslate"><span class="pl-s1">project</span>@<span class="pl-c1">1.0</span><span class="pl-c1">.0</span>
<span class="pl-s1">├──</span> <span class="pl-s1">babel</span><span class="pl-c1">-</span><span class="pl-s1">runtime</span>@<span class="pl-c1">5.1</span><span class="pl-c1">.9</span>
<span class="pl-s1">└──</span> <span class="pl-s1">sub</span><span class="pl-c1">-</span><span class="pl-s1">project</span>@<span class="pl-c1">1.0</span><span class="pl-c1">.0</span> <span class="pl-c1">-</span><span class="pl-c1">&gt;</span> <span class="pl-pds"><span class="pl-c1">/</span>Users<span class="pl-c1">/</span>fc</span><span class="pl-c1">/</span><span class="pl-v">Development</span><span class="pl-c1">/</span><span class="pl-s1">sub</span><span class="pl-c1">-</span><span class="pl-s1">project</span></pre></div>
<p dir="auto"><strong>Notice:</strong> <code class="notranslate">babel-runtime</code> still doesn't appear under <code class="notranslate">sub-project</code>.</p>
<p dir="auto">But building the project again will duplicate its dependencies:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="[0] multi main 52 bytes {0} [built]
[1] ./index.js 2.08 kB {0} [built]
[2] ./~/babel-runtime/helpers/interop-require-wildcard.js 148 bytes {0} [built]
[3] /Users/fc/Development/sub-project/index.js 2.46 kB {0} [built]
[4] /Users/fc/Development/sub-project/~/babel-runtime/helpers/interop-require-wildcard.js 148 bytes {0} [built]"><pre class="notranslate"><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span> <span class="pl-s1">multi</span> <span class="pl-s1">main</span> <span class="pl-c1">52</span> <span class="pl-s1">bytes</span> <span class="pl-kos">{</span><span class="pl-c1">0</span><span class="pl-kos">}</span> <span class="pl-kos">[</span><span class="pl-s1">built</span><span class="pl-kos">]</span>
<span class="pl-kos">[</span><span class="pl-c1">1</span><span class="pl-kos">]</span> <span class="pl-kos">.</span><span class="pl-c1">/</span><span class="pl-s1">index</span><span class="pl-kos">.</span><span class="pl-c1">js</span> <span class="pl-c1">2.08</span> <span class="pl-s1">kB</span> <span class="pl-kos">{</span><span class="pl-c1">0</span><span class="pl-kos">}</span> <span class="pl-kos">[</span><span class="pl-s1">built</span><span class="pl-kos">]</span>
<span class="pl-kos">[</span><span class="pl-c1">2</span><span class="pl-kos">]</span> <span class="pl-kos">.</span><span class="pl-c1">/</span><span class="pl-c1">~</span><span class="pl-pds"><span class="pl-c1">/</span>babel-runtime<span class="pl-c1">/</span>helpers</span><span class="pl-c1">/</span><span class="pl-s1">interop</span><span class="pl-c1">-</span><span class="pl-en">require</span><span class="pl-c1">-</span><span class="pl-s1">wildcard</span><span class="pl-kos">.</span><span class="pl-c1">js</span> <span class="pl-c1">148</span> <span class="pl-s1">bytes</span> <span class="pl-kos">{</span><span class="pl-c1">0</span><span class="pl-kos">}</span> <span class="pl-kos">[</span><span class="pl-s1">built</span><span class="pl-kos">]</span>
<span class="pl-kos">[</span><span class="pl-c1">3</span><span class="pl-kos">]</span> <span class="pl-c1">/</span><span class="pl-v">Users</span><span class="pl-c1">/</span><span class="pl-s1">fc</span><span class="pl-c1">/</span><span class="pl-v">Development</span><span class="pl-c1">/</span><span class="pl-s1">sub</span><span class="pl-c1">-</span><span class="pl-s1">project</span><span class="pl-c1">/</span><span class="pl-s1">index</span><span class="pl-kos">.</span><span class="pl-c1">js</span> <span class="pl-c1">2.46</span> <span class="pl-s1">kB</span> <span class="pl-kos">{</span><span class="pl-c1">0</span><span class="pl-kos">}</span> <span class="pl-kos">[</span><span class="pl-s1">built</span><span class="pl-kos">]</span>
<span class="pl-kos">[</span><span class="pl-c1">4</span><span class="pl-kos">]</span> <span class="pl-c1">/</span><span class="pl-v">Users</span><span class="pl-c1">/</span><span class="pl-s1">fc</span><span class="pl-c1">/</span><span class="pl-v">Development</span><span class="pl-c1">/</span><span class="pl-s1">sub</span><span class="pl-c1">-</span><span class="pl-s1">project</span><span class="pl-c1">/</span><span class="pl-c1">~</span><span class="pl-pds"><span class="pl-c1">/</span>babel-runtime<span class="pl-c1">/</span>helpers</span><span class="pl-c1">/</span><span class="pl-s1">interop</span><span class="pl-c1">-</span><span class="pl-en">require</span><span class="pl-c1">-</span><span class="pl-s1">wildcard</span><span class="pl-kos">.</span><span class="pl-c1">js</span> <span class="pl-c1">148</span> <span class="pl-s1">bytes</span> <span class="pl-kos">{</span><span class="pl-c1">0</span><span class="pl-kos">}</span> <span class="pl-kos">[</span><span class="pl-s1">built</span><span class="pl-kos">]</span></pre></div>
<p dir="auto">Using <code class="notranslate">resolve.root</code> or changing the resolve scope of loaders do not change anything.</p>
<h2 dir="auto">Expectations</h2>
<p dir="auto">It would link to be able the link my in-development modules into my project using <code class="notranslate">npm link</code> without having to tweak the webpack config.</p>
<p dir="auto"><strong>The real problem isn't the build size but the unexpected behaviors raised but duplicated dependencies.</strong></p>