title: How do I group functions in ES6 Modules?
body: <p>How do you group functions in a ES6 Module package? In CommonJS packages you can do this.</p>
  <pre><code>// package.js
  const startSession = require(&quot;./startSession&quot;);
  
  module.exports = {
    hooks: {
      startSession,
    },
  };
  
  
  // client.js
  const { hooks: {startSession } } = require(&quot;package&quot;);
  // OR
  const { startSession } = require(&quot;package&quot;).hooks;
  </code></pre>
  <p>It looks like the similar syntax in ES6 code isn't supported.</p>
  <pre><code>import { hooks: {startSession } } from &quot;packageX&quot;;
  </code></pre>
  <p>On source for that, is this babel issue from 2016. <a href="https://github.com/babel/babel/issues/4996" rel="nofollow noreferrer">https://github.com/babel/babel/issues/4996</a></p>
  <p>Another is the official documentation, that doesn't really mentioning this kind of syntax.
  <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import</a></p>
  <p><strong>Solutions</strong></p>
  <p>One solutions in ES6 javascript is this.</p>
  <pre><code>// package.js
  import startSession from &quot;./hooks/start-session&quot;;
  export const hooks = {
    startSession
  };
  
  // client.js
  import { hooks } from &quot;package&quot;;
  const { startSession } = hooks;
  </code></pre>
  <p>But I would like to write the import in one line. Maybe not possible?</p>
  <p>This is not what I look for.</p>
  <pre><code>import { hooks } from &quot;package&quot;; const { startSession } = hooks;
  </code></pre>
  <p>This kind of syntax would be nice, but how do you set up the packages, if possible?</p>
  <pre><code>import { startSession } from &quot;packageX/hooks&quot;;
  import { startSession } from &quot;packageX&quot;.hooks;
  </code></pre>
  <p>Any other suggestions?</p>
  <p><strong>Answer</strong></p>
  <p>Estus below pushed me in the right direction. Here is a more detailed answer.</p>
  <pre><code>// package.json
  &quot;main&quot;: &quot;src/index.js&quot;,
  &quot;exports&quot;: {
      &quot;.&quot;: &quot;./src/index.js&quot;,
      &quot;./hooks&quot;: &quot;./src/hooks/index.js&quot;,
  }
  
  // src/hooks/start-session.js
  export default () =&gt; {}
  
  // src/hooks/index.js
  export * as startSession from &quot;./start-session&quot;;
  
  // src/index.js
  export const doSomething () =&gt; {}
  
  // client.js
  import { doSomething } from &quot;packageX&quot;;
  import { startSession } from &quot;packageX/hooks&quot;;
  </code></pre>