title: Import exports from CJS module as default with @rollup/plugin-commonjs
body: <p>I'm using a javascript library that's written for cjs. I am  also using @rollup/plugin-commonjs to transform that library so that I can import it.</p>
  <pre class="lang-js prettyprint-override"><code>// a file I want to import from cjs library
  exports.x = 5
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>// rollup.config.js
  resolve({
    browser: true,
    dedupe: ['svelte'],
    preferBuiltins: false,
  }),
  commonjs({
    include: [
      &quot;node_modules/**&quot;,
    ],
    dynamicRequireTargets: [  
      &quot;node_modules/cjs-lib/**&quot;,
    ],
  }),
  </code></pre>
  <p>I'm trying to use it in my own app like this.</p>
  <pre class="lang-js prettyprint-override"><code>import lib from &quot;cjs-lib/file.js&quot;
  lib.z = function() {
    return lib.x + 5;
  }
  </code></pre>
  <p>An issue occurs there, with</p>
  <pre class="lang-js prettyprint-override"><code>Uncaught TypeError: lib.x is not a function
  </code></pre>
  <p>It seems all the methods defined in the cjs library like <code>exports.x</code> is undefined when imported.</p>
  <p>Is there anyway to configure <code>@rollup/plugin-commonjs</code> such that the <code>exports</code> object originally in the cjs library becomes the default export object when its imported in my app?</p>
  <p><strong>Edit:</strong></p>
  <p>To be more concrete, the exact library I'm using is CodeMirror and I'm trying to use its addon, <a href="https://www.unpkg.com/browse/codemirror@5.59.0/addon/runmode/runmode.node.js" rel="nofollow noreferrer"><code>runmode/runmode.node.js</code></a>.</p>
  <pre class="lang-js prettyprint-override"><code>import CodeMirror from 'codemirror/addon/runmode/runmode.node';
  import 'codemirror/mode/meta';
  
  // Try to extend it here, but findModeByExtension is undefined.
  CodeMirror.findMode = function (mode) {
      return mode &amp;&amp; (CodeMirror.findModeByExtension(mode) ||
          CodeMirror.findModeByFileName(mode) ||
          CodeMirror.findModeByName(mode) ||
          CodeMirror.findModeByMIME(mode) ||
          CodeMirror.findModeByMode(mode));
  };
  </code></pre>
  <p>With rollup, the <code>CodeMirror</code> object imported is empty, <code>{}</code>. But with webpack, the <code>CodeMirror</code> object contains all the methods assigned to it with <code>exports.x = y</code> and <code>exports[a] = b</code> in the <code>runmode.node.js</code> file.</p>
  <p><strong>Edit:</strong></p>
  <p>Surprisingly, if the library is imported within <code>rollup.config.js</code>, the imported object is not empty and contains all the methods expected.</p>
  <pre class="lang-js prettyprint-override"><code>// rollup.config.js
  import * as CodeMirror from &quot;codemirror/addon/runmode/runmode.node&quot;
  console.log(&quot;CodeMirror&quot;, CodeMirror);
  
  runmode [Object: null prototype] {
    StringStream: [Getter],
    countColumn: [Getter], ...
  </code></pre>
  <p><strong>Edit</strong>:</p>
  <p>As it stands my <code>rollup.config.js</code> file has</p>
  <pre class="lang-js prettyprint-override"><code>        commonjs({
              include: &quot;node_modules/**&quot;,
              dynamicRequireTargets: [&quot;node_modules/codemirror/**&quot;],
          }),
  </code></pre>
  <p>Without including <code>codemirror</code> in the <code>dynamicRequireTargets</code>, it results in a <code>require is undefined</code> error when it comes to these lines within the <code>runmode.node.js</code>:</p>
  <pre class="lang-js prettyprint-override"><code>require.cache[require.resolve(&quot;../../lib/codemirror&quot;)] = require.cache[require.resolve(&quot;./runmode.node&quot;)];
  require.cache[require.resolve(&quot;../../addon/runmode/runmode&quot;)] = require.cache[require.resolve(&quot;./runmode.node&quot;)];
  </code></pre>
  <p><code>dynamicRequireTargets</code> seems to fix this by wrapping the entire file in this</p>
  <pre class="lang-js prettyprint-override"><code>const commonjsRegister = require('commonjsHelpers.js?commonjsRegister');
  commonjsRegister(&quot;/$$rollup_base$$/node_modules/codemirror/addon/runmode/runmode.node.js&quot;, function (module, exports) {
    'use strict';
    // runmode.node.js
  }
  </code></pre>
  <p>However, this removes all exports and hence the empty import. Is there a way to use <code>dynamicRequireTargets</code> and cjs exports together?</p>