title: ASYNC / AWAIT SyntaxError: await is only valid in async functions and the top level bodies of modules
body: <p>I am doing some really simple testing regarding reading csv files into a json format using the <a href="https://www.npmjs.com/package/csvtojson#from-csv-file-to-json-array" rel="noreferrer" title="NPM CSVTOJSON">csvtojson node module</a>, I used the code below as a template</p>
  <pre><code>a,b,c
  1,2,3
  4,5,6
  */
  const csvFilePath='&lt;path to csv file&gt;'
  const csv=require('csvtojson')
  csv()
  .fromFile(csvFilePath)
  .then((jsonObj)=&gt;{
      console.log(jsonObj);
      /**
       * [
       *  {a:&quot;1&quot;, b:&quot;2&quot;, c:&quot;3&quot;},
       *  {a:&quot;4&quot;, b:&quot;5&quot;. c:&quot;6&quot;}
       * ]
       */ 
  })
   
  // Async / await usage
  const jsonArray=await csv().fromFile(csvFilePath);
  </code></pre>
  <p>I am mostly focusing on the</p>
  <blockquote>
  <p>// Async / await usage</p>
  </blockquote>
  <blockquote>
  <p>const jsonArray=await csv().fromFile(csvFilePath);</p>
  </blockquote>
  <p>Section of the code. Right so here is my code</p>
  <pre><code>// const JSONtoCSV = require(&quot;json2csv&quot;)
  // const FileSystem = require(&quot;fs&quot;)
  
  async function test()
  {
      const data = await CSVtoJSON().fromFile('./input.csv')
      return data
  }
  
  let temp = await test()
  
  console.log(temp)
  </code></pre>
  <p>and ever which way I have tried it I always get the following error</p>
  <pre><code>let temp = await test()
             ^^^^^
  
  SyntaxError: await is only valid in async functions and the top level bodies of modules
      at Object.compileFunction (node:vm:352:18)
      at wrapSafe (node:internal/modules/cjs/loader:1031:15)
      at Module._compile (node:internal/modules/cjs/loader:1065:27)
      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
      at Module.load (node:internal/modules/cjs/loader:981:32)
      at Function.Module._load (node:internal/modules/cjs/loader:822:12)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
      at node:internal/main/run_main_module:17:47
  </code></pre>
  <p>or</p>
  <pre><code>const data = await CSVtoJSON().fromFile('./input.csv');
               ^^^^^
  
  SyntaxError: await is only valid in async functions and the top level bodies of modules
      at Object.compileFunction (node:vm:352:18)
      at wrapSafe (node:internal/modules/cjs/loader:1031:15)
      at Module._compile (node:internal/modules/cjs/loader:1065:27)
      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
      at Module.load (node:internal/modules/cjs/loader:981:32)
      at Function.Module._load (node:internal/modules/cjs/loader:822:12)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
      at node:internal/main/run_main_module:17:47
  </code></pre>
  <p>if I switch the code to be all top level like so</p>
  <pre><code>const CSVtoJSON = require(&quot;csvtojson&quot;)
  // const JSONtoCSV = require(&quot;json2csv&quot;)
  // const FileSystem = require(&quot;fs&quot;)
  
  const data = await CSVtoJSON().fromFile('./input.csv')
  
  console.log(data)
  </code></pre>
  <p>I can't see why this isn't working.</p>
  <p>EDIT: I made the change as @tasobu noted. Now all I get is a returned promise</p>
  <pre><code>const data = (async () =&gt; {
      return await CSVtoJSON().fromFile('./input.csv')
  })
  
  console.log(data)
  </code></pre>
  <pre><code>Debugger attached.
  Promise { &lt;pending&gt; }
  Waiting for the debugger to disconnect...
  </code></pre>