title: NodeJS --> SyntaxError: Cannot use import statement outside a module
body: <p>I am a beginner to NodeJS. I was trying to import a module <code>fs.readFileSync</code>
  I used import { readFileSync } from 'fs' as per the NodeJS docs,
  But I keep getting the below error,</p>
  <p><strong>ERROR</strong>-----&gt;&gt; <code>SyntaxError: Cannot use import statement outside a module</code></p>
  <p><strong>My Research</strong></p>
  <p>-&gt;I found that adding <code>type:module</code> in <code>package.json</code> file solves this issue.</p>
  <p><strong>My Doubt</strong></p>
  <p>-&gt;I want to know why this cannot be imported as per documentation??</p>
  <p>Code</p>
  <pre><code>import { readFileSync } from 'fs';
  fs.writeFileSync('notes.txt','This was read by NodeJS')
  </code></pre>
  <p><strong>Full error</strong></p>
  <pre><code>SyntaxError: Cannot use import statement outside a module
      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:81:12)
      at node:internal/main/run_main_module:17:47
  </code></pre>