15

Why do I get errors like these when I try to use the new Node.js support for ES6 modules (e.g. with node --experimental-modules script.mjs)?

// script.mjs
import * as fs from 'fs';

// TypeError: fs.readFile is not a function
fs.readFile('data.csv', 'utf8', (err, data) => {
    if (!err) {
        console.log(data);
    }
});
// TypeError: fs.readdirSync is not a function
fs.readdirSync('.').forEach(fileName => {
    console.log(fileName);
});
Qwertie
  • 16,354
  • 20
  • 105
  • 148

2 Answers2

30

You must use import fs from 'fs', not import * as fs from 'fs'.

This is because (at least from the point of view of mjs files) the 'fs' module exports only one thing, which is called default. So if you write import * as fs from 'fs', fs.default.readFile exists but fs.readFile does not. Perhaps the same is true of all Node.js (CommonJS) modules.

Confusingly, in TypeScript modules (with @types/node and ES5 output), import fs from 'fs' produces error

error TS1192: Module '"fs"' has no default export

so in TypeScript you must write import * as fs from 'fs'; by default. It appears this can be changed to match the way mjs files work using the new "esModuleInterop": true option in tsconfig.json.

Qwertie
  • 16,354
  • 20
  • 105
  • 148
  • 1
    `fs` is a core module in node, you don't need to import it at all. It's always there. – Tomalak Jun 02 '18 at 22:05
  • 7
    Failure to import fs will cause `ReferenceError: fs is not defined`, even if it's not an .mjs module. – Qwertie Jun 04 '18 at 01:27
  • TypeScript is for type safety, so you should import your types. It's similar to using `var fs = require('fs');` but we have to do some trickery for TS to get it just right. I was able to get it using `Import * as fs from 'fs';`, but not `Import fs from 'fs';`. I have been able to do the latter with most node modules when enabling the `esModuleInterop` and `allowSyntheticDefaultImports`, but not fs :( This one does work, for example: `import path from 'path';` – ps2goat Apr 20 '21 at 23:28
5

We can simply import like this it in our code

import * as fs from 'fs';

and it is perfectly working for me, give it a try

surender pal
  • 447
  • 6
  • 15