Type Error Read file is not defined even after importing the module

The following code is written in node version 8

const fs = require('fs').promises
const [, pairA] = require('./pairs.json')

const accountASignsTx = async (secret) => {
    const fundsReleaseTx = await fs.readFile('./fundsReleaseTx.x', { encoding: 'base64'

I have imported readfile module but getting the following error

TypeError: Cannot read property 'readFile' of undefined

2) I taught of removing .promises then it coudn't handle await please help me

2 answers

  • answered 2018-11-08 07:54 SET

    I think your problem is that you exporting not the whole fs module, but some promises as fs module. Try to change

    const fs = require('fs').promises


    const fs = require('fs')

    also fs.readFile as asynchronous function but it does not return promise, so you can't await it. It have a callback parameter with will be executed when function will finish execution. So you can wrap it in Promise to wait it with await:

    await new Promise(resolve=>
        fs.readFile('./fundsReleaseTx.x', { encoding: 'base64' },

    or you can just use fs.readFileSync which is blocking further code execution until it finish its work so you don't need any additional tricks to await it.

  • answered 2018-11-08 08:20 Nelson Owalo

    I stand corrected, but AFAIK, the FS Promises API was added in nodejs V10.x:

    You will have to upgrade nodejs to version 10.x or higher if you want that function to work. Also note that its behind the experimental flag, so you will have to deal with that.