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