Savad
Savad

Reputation: 1316

fs.createwritestream is not a function

I am Trying to develop a react app that help me to download files to my amazon cloud. For that i used This module

download-file-with-progressbar

Here is my code:

      var dl            = require('download-file-with-progressbar');
                //-----------Download File-------------------
                option = {
                    filename: 'fileName',
                    dir: os.tmpdir(),
                    onDone: (info)=>{
                    console.log('done', info);
                    },
                    onError: (err) => {
                    console.log('error', err);
                    },
                    onProgress: (curr, total) => {
                    console.log('progress', (curr / total * 100).toFixed(2) + '%');
                    },
                }

                dd = dl('https://nodejs.org/dist/v6.10.2/node-v6.10.2-linux-x64.tar.gz', this.option);
                //-------------------------------------------

It is working fine on nodejs project. But when i used it in a react project(created using 'create-react-app') it produced following error.

TypeError: fs.createWriteStream is not a function

My idea of the project is to create a react app that help to download files to my server or cloud space. how to fix that error.

Upvotes: 2

Views: 8390

Answers (1)

Arnaud Christ
Arnaud Christ

Reputation: 3551

The createWriteStream is a function part of the Nodejs API, that is why it is working fine when you run it as a nodejs project.

But when you run a React app, it is your browser that executes your code and not nodejs. And browsers don't know about this API.

If you want to download a file from a React application, you have to do it through an HTTP request. You can use the fetch API for example.

Upvotes: 7

Related Questions