Why does fs.writeFileSync() throws "is not a function" error?

I am trying to save some data in a file using fs.writeFileSync, but it doesn’t work and I cannot figure out why. It throws the following error: Unhandled Rejection (TypeError): fs.writeFileSync is not a function

In my app I use it like this :

const fs = require('fs');
const stateFile = "./usersStateFile";

const saveLastEventSequenceId = (sequenceId) => {
    try {
            fs.writeFileSync(stateFile, sequenceId);
        } catch (err) {
          throw err;
        }
     };

The sequenceId is a number and the ./usersStateFile doesn’t exist, but fs.writeFileSync() should create it if it doesn’t exist.

What might be the problem ?

75 thoughts on “Why does fs.writeFileSync() throws "is not a function" error?”

Leave a Comment