Node: fs.createReadStream is not a function when using for await

Created on 18 May 2018  路  4Comments  路  Source: nodejs/node

  • Version: v10.1.0
  • Platform: Darwin YLT160024.local 17.5.0 Darwin Kernel Version 17.5.0: Fri Apr 13 19:32:32 PDT 2018; root:xnu-4570.51.2~1/RELEASE_X86_64 x86_64

Hi I testing new "for await" feature with express.js to send data to client chunk by chunk.
Here is my first example when I getting error

const express = require('express')
const fs = require('fs')
const app = express()

app.get('/', (req, res) => {
  const stream = fs.createReadStream('a.json', {
    highWaterMark: 5
  })
  (async () => {
    for await (const chunk of stream) {
      res.write(`Read ${chunk}`)
    }
  })()
  res.end('Hello World!')
})

app.listen(3000)

Here is my error
TypeError: fs.createReadStream(...) is not a function

Here is working example.

const express = require('express')
const fs = require('fs')
const app = express()

app.get('/', async (req, res) => {
  const stream = fs.createReadStream('a.json', {
    highWaterMark: 5
  })
  for await (const chunk of stream) {
    res.write(`Read ${chunk}`)
  }
  res.send('Hello World!')
})

app.listen(3000)

Please help to understand why fs.createReadStream is not a function in first example.

question

Most helpful comment

Please help to understand why fs.createReadStream is not a function in first example.

The error message says that fs.createReadStream(...) is not a function, so it鈥檚 not about fs.createReadStream, it鈥檚 about the result of fs.createReadStream(...).

What you鈥檙e essentially writing is a piece of code that has this format:

const a = b(c)
(async () => d)()

which is parsed as const a = b(c)(async () = > d)(), because you don鈥檛 use semicolons; rather than calling b(c) and the async function, it tries to call the function b(c) with the async function as an argument.

Here and here are some more details.

tl;dr: If you don鈥檛 use semicolons, you need to prefix lines that start with any of the characters [, (, `, +, *, /, -, ,, . with a semicolon.

I鈥檓 closing this since it鈥檚 not a bug with Node.js, but if you have more question feel free to raise them at https://github.com/nodejs/help. :)

All 4 comments

Also this just not working, I don't know why.
Only prints "1" one time.

const express = require('express')
const fs = require('fs')
const app = express()

function wait(t) {
  return new Promise(r => {
    setTimeout(t, r);
  })
}

app.get('/', async (req, res) => {
  const stream = fs.createReadStream('a.json', {
    highWaterMark: 5
  })
  for await (const chunk of stream) {
    console.log(1);
    await wait(500);
    console.log(2);
    res.write(`Read ${chunk}`);
  }
  res.end('Hello World!');
})

app.listen(3000)

Please help to understand why fs.createReadStream is not a function in first example.

The error message says that fs.createReadStream(...) is not a function, so it鈥檚 not about fs.createReadStream, it鈥檚 about the result of fs.createReadStream(...).

What you鈥檙e essentially writing is a piece of code that has this format:

const a = b(c)
(async () => d)()

which is parsed as const a = b(c)(async () = > d)(), because you don鈥檛 use semicolons; rather than calling b(c) and the async function, it tries to call the function b(c) with the async function as an argument.

Here and here are some more details.

tl;dr: If you don鈥檛 use semicolons, you need to prefix lines that start with any of the characters [, (, `, +, *, /, -, ,, . with a semicolon.

I鈥檓 closing this since it鈥檚 not a bug with Node.js, but if you have more question feel free to raise them at https://github.com/nodejs/help. :)

@nairihar For your second comment, the issue is that setTimeout takes its function argument as the first one, not the second one. This is different from other Node.js functions because it鈥檚 not a Node.js API. You should be seeing an error somewhere as a result of that.

Thank you @addaleax , I confused setTimeout example.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcollina picture mcollina  路  3Comments

dfahlander picture dfahlander  路  3Comments

loretoparisi picture loretoparisi  路  3Comments

Brekmister picture Brekmister  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments