title: how to use top level async await with typescript?
body: <p>I've a typescript project, when i use <code>await xxx.someAsyncMethod(..)</code> at the top level, typescript linter shows,</p>
  <pre><code>Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
  </code></pre>
  <p>so I'll change the module to esnext <code>&quot;module&quot;: &quot;esnext&quot;</code></p>
  <p>and i'll go ahead and try running the program <code>npx ts-node app/app.ts</code></p>
  <p>then console informs me</p>
  <pre><code>(node:15509) Warning: To load an ES module, set &quot;type&quot;: &quot;module&quot; in the package.json or use the .mjs extension.
  Cannot use import statement outside a module
  </code></pre>
  <p>upon setting the type to module in package.json <code>&quot;type&quot;: &quot;module&quot;</code> when i run the program the console out puts</p>
  <pre><code>TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension &quot;.ts&quot; for /Users/apple/Desktop/source_given_to_me/Git/bots/wab/app/app.ts
      at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
      at Loader.getFormat (internal/modules/esm/loader.js:113:42)
      at Loader.getModuleJob (internal/modules/esm/loader.js:244:31)
      at Loader.import (internal/modules/esm/loader.js:178:17)
  </code></pre>
  <p>what can be done so i can get away with using top level async await?</p>
  <p><strong>EDIT:</strong>
  my redis.ts file</p>
  <pre><code>
  import * as pm from &quot;child_process&quot;
  var prc:pm.ChildProcess;
  
  async function startRedis(path:string): Promise&lt;boolean&gt; {
      return new Promise((resolve) =&gt; {
          console.log('starting redis')
          var rp = pm.spawn(path)
          rp.stdout.on(&quot;data&quot;, (data) =&gt; {
              console.log(data)
              console.log('redis fired up')
              prc= rp
              resolve(true)
          })
          rp.stderr.on(&quot;data&quot;, (data) =&gt; {
              console.log(data)
              console.log('redis failed to start')
              resolve(false)
          })
      })
  }
  
  function active():boolean{
      return prc?!prc.killed:false
  }
  
  async function stopRedis(): Promise&lt;boolean&gt; {
      return new Promise(
          (resolve)=&gt;{
              if(prc){
                  resolve(prc.kill(&quot;SIGTERM&quot;))
              } else {
                  resolve(false)
              }
          }
      )
  }
  
  
  export const redis = {
      startRedis,
      stopRedis,
      active
  }
  
  
  </code></pre>
  <p>my config.ts file (tsconfig.json is below)</p>
  <pre><code>export const config = {
      redisPath:'../r607/src/redis-server'
  }
  </code></pre>
  <p>my app.ts file</p>
  <pre><code>import {config} from &quot;./config.js&quot;
  import {redis} from &quot;./redis.js&quot;
  //import * as pm from &quot;child_process&quot;
  
  await redis.startRedis(config.redisPath)
  if(!redis.active()){
      process.abort()
  }
  </code></pre>
  <p>my tsconfig.json</p>
  <pre><code>{
    &quot;compilerOptions&quot;: {
      /* Visit https://aka.ms/tsconfig.json to read more about this file */
  
      /* Basic Options */
      // &quot;incremental&quot;: true,                   /* Enable incremental compilation */
      &quot;target&quot;: &quot;esnext&quot;,                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
      &quot;module&quot;: &quot;ESNext&quot;,                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
      // &quot;lib&quot;: [],                             /* Specify library files to be included in the compilation. */
      // &quot;allowJs&quot;: true,                       /* Allow javascript files to be compiled. */
      // &quot;checkJs&quot;: true,                       /* Report errors in .js files. */
      // &quot;jsx&quot;: &quot;preserve&quot;,                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
      // &quot;declaration&quot;: true,                   /* Generates corresponding '.d.ts' file. */
      // &quot;declarationMap&quot;: true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
      // &quot;sourceMap&quot;: true,                     /* Generates corresponding '.map' file. */
      // &quot;outFile&quot;: &quot;./&quot;,                       /* Concatenate and emit output to single file. */
       &quot;outDir&quot;: &quot;./build&quot;,                        /* Redirect output structure to the directory. */
      // &quot;rootDir&quot;: &quot;./&quot;,                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
      // &quot;composite&quot;: true,                     /* Enable project compilation */
      // &quot;tsBuildInfoFile&quot;: &quot;./&quot;,               /* Specify file to store incremental compilation information */
      // &quot;removeComments&quot;: true,                /* Do not emit comments to output. */
      // &quot;noEmit&quot;: true,                        /* Do not emit outputs. */
      // &quot;importHelpers&quot;: true,                 /* Import emit helpers from 'tslib'. */
      // &quot;downlevelIteration&quot;: true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
      // &quot;isolatedModules&quot;: true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
  
      /* Strict Type-Checking Options */
      &quot;strict&quot;: true,                           /* Enable all strict type-checking options. */
      // &quot;noImplicitAny&quot;: true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
      // &quot;strictNullChecks&quot;: true,              /* Enable strict null checks. */
      // &quot;strictFunctionTypes&quot;: true,           /* Enable strict checking of function types. */
      // &quot;strictBindCallApply&quot;: true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
      // &quot;strictPropertyInitialization&quot;: true,  /* Enable strict checking of property initialization in classes. */
      // &quot;noImplicitThis&quot;: true,                /* Raise error on 'this' expressions with an implied 'any' type. */
      // &quot;alwaysStrict&quot;: true,                  /* Parse in strict mode and emit &quot;use strict&quot; for each source file. */
  
      /* Additional Checks */
      // &quot;noUnusedLocals&quot;: true,                /* Report errors on unused locals. */
      // &quot;noUnusedParameters&quot;: true,            /* Report errors on unused parameters. */
      // &quot;noImplicitReturns&quot;: true,             /* Report error when not all code paths in function return a value. */
      // &quot;noFallthroughCasesInSwitch&quot;: true,    /* Report errors for fallthrough cases in switch statement. */
  
      /* Module Resolution Options */
      // &quot;moduleResolution&quot;: &quot;node&quot;,            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
      // &quot;baseUrl&quot;: &quot;./&quot;,                       /* Base directory to resolve non-absolute module names. */
      // &quot;paths&quot;: {},                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
      // &quot;rootDirs&quot;: [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
      // &quot;typeRoots&quot;: [],                       /* List of folders to include type definitions from. */
      // &quot;types&quot;: [],                           /* Type declaration files to be included in compilation. */
      // &quot;allowSyntheticDefaultImports&quot;: true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
      &quot;esModuleInterop&quot;: true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
      // &quot;preserveSymlinks&quot;: true,              /* Do not resolve the real path of symlinks. */
      // &quot;allowUmdGlobalAccess&quot;: true,          /* Allow accessing UMD globals from modules. */
  
      /* Source Map Options */
      // &quot;sourceRoot&quot;: &quot;&quot;,                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
      // &quot;mapRoot&quot;: &quot;&quot;,                         /* Specify the location where debugger should locate map files instead of generated locations. */
      // &quot;inlineSourceMap&quot;: true,               /* Emit a single file with source maps instead of having a separate file. */
      // &quot;inlineSources&quot;: true,                 /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
  
      /* Experimental Options */
      // &quot;experimentalDecorators&quot;: true,        /* Enables experimental support for ES7 decorators. */
      // &quot;emitDecoratorMetadata&quot;: true,         /* Enables experimental support for emitting type metadata for decorators. */
  
      /* Advanced Options */
      &quot;skipLibCheck&quot;: true,                     /* Skip type checking of declaration files. */
      &quot;forceConsistentCasingInFileNames&quot;: true  /* Disallow inconsistently-cased references to the same file. */
    }
  }
  </code></pre>