title: CommonJS wrapper for ES module scripts
body: <p>The <a href="https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_approach_1_use_an_es_module_wrapper" rel="nofollow noreferrer">Node documentation on ECMAScript Modules</a> makes recommendations for "dual packages" that provide both CommonJS scripts and ES module scripts to users.</p>
  
  <p>Specifically, they suggest transpiling ES module scripts to CommonJS, then using an "ES module wrapper", an ES module script that imports from CJS and re-exports its CJS named exports as ESM named exports.</p>
  
  <pre class="lang-js prettyprint-override"><code>// ./node_modules/pkg/wrapper.mjs
  import cjsModule from './index.cjs';
  export const name = cjsModule.name;
  </code></pre>
  
  <p>My question is: is it possible to do this the opposite way, providing my scripts in ES module format, but adding a simple wrapper file in CommonJS allowing CJS users to <code>require</code> it?</p>
  
  <p>For my part, I can't see a way to do this in synchronous code. It appears to me that CommonJS scripts can only import ES modules via asynchronous dynamic <code>import()</code>.</p>
  
  <pre class="lang-js prettyprint-override"><code>(async () =&gt; {
      const {foo} = await import('./foo.mjs');
  })();
  </code></pre>
  
  <p>But that means my CJS script can't export anything that depends on <code>foo</code>.</p>