title: How to module.export a value defined inside an async function?
body: <p>I have the below code and want to export the <code>handler</code> using <code>module.exports.handler</code>. Since <code>start()</code> is an <strong>async function</strong> and the <strong>handler is defined inside</strong> of it, I can't export it properly by using the below code. I tried using <strong>top-level await</strong> for start() and <a href="https://stackoverflow.com/questions/68126382/why-top-level-package-jsons-type-module-is-affecting-the-packages-inside-n">got an error</a>. Can I know another way to achieve this? Thanks in advance.</p>
  <pre><code>//index.ts
  
  import serverless from 'serverless-http';
  
  let handler;
  const start = async (): Promise&lt;void&gt; =&gt; {
      
      //some function calls with await
  
      const server = express();
      //server.use()
  
      handler = serverless(server);
  };
  
  start().then(() =&gt; {
      console.log(handler) //[AsyncFunction (anonymous)]
  });
  console.log(handler) //undefined
  
  module.exports.handler = handler;
  </code></pre>
  <p>Btw, I need to use <code>handler</code> in my <code>serverless.yml</code></p>
  <pre><code>service: rest-server
  provider:
    name: aws
    runtime: nodejs12.x
    region: eu-west-1
    environment: ${file(serverless.env.yml)
    stage: dev
    memorySize: 128
  functions:
    h5p:
      handler: src/index.handler
      events:
        - http: ANY /
        - http: 'ANY {proxy+}'
  plugins:
    - serverless-plugin-typescript
    - serverless-offline
  </code></pre>