fs.createWriteStream does not immediately create file?

fs.createwritestream is not a function
fs createwritestream is not a function nodejs
fs writefile does not create file
fs createwritestream create directory
fs create file
fs createreadstream dest on is not a function
node fs
fs promises createwritestream

I have made a simple download from http function as below (error handling is omitted for simplifcation):

function download(url, tempFilepath, filepath, callback) {
    var tempFile = fs.createWriteStream(tempFilepath);
    http.request(url, function(res) {
        res.on('data', function(chunk) {
            tempFile.write(chunk);
        }).on('end', function() {
            tempFile.end();
            fs.renameSync(tempFile.path, filepath);
            return callback(filepath);
        })
    });
}

However, as I call download() tens of times asynchronously, it seldom reports error on fs.renameSync complaining it cannot find file at tempFile.path.

Error: ENOENT, no such file or directory 'xxx'

I used the same list of urls to test it, and it failed about 30% of time. The same list of urls worked when downloaded one by one.

Testing some more, I found out that the following code

fs.createWriteStream('anypath');
console.log(fs.exist('anypath'));
console.log(fs.exist('anypath'));
console.log(fs.exist('anypath'));

does not always print true, but sometimes the first answer prints false.

I am suspecting that too many asynchronous fs.createWriteStream calls cannot guarantee the file creation. Is this true? Are there any methods to guarantee file creation?

You shouldn't call write on your tempFile write stream until you've received the 'open' event from the stream. The file won't exist until you see that event.

For your function:

function download(url, tempFilepath, filepath, callback) {
    var tempFile = fs.createWriteStream(tempFilepath);
    tempFile.on('open', function(fd) {
        http.request(url, function(res) {
            res.on('data', function(chunk) {
                tempFile.write(chunk);
            }).on('end', function() {
                tempFile.end();
                fs.renameSync(tempFile.path, filepath);
                return callback(filepath);
            });
        });
    });
}

For your test:

var ws = fs.createWriteStream('anypath');
ws.on('open', function(fd) {
    console.log(fs.existsSync('anypath'));
    console.log(fs.existsSync('anypath'));
    console.log(fs.existsSync('anypath'));
});

fs.createWriteStream is not creating the file · Issue #406 · nodejs , I am trying to create the file using fs.createWriteStream but it's not working. Here is the code. try{ var path = "js/A/A.js"; //(js/A/ is an existing  It's supposed to write the contents of the file specified within the url to a new file on my computer using fs.createWriteStream(). It creates the file on my computer, but the problem is is that the file is empty. It doesn't actually "write" the contents of the file specified with the url to the new file.

The accepted answer didn't download some of the last bytes for me. Here's a Q version that works correctly (but without the temp file).

'use strict';

var fs = require('fs'),
    http = require('http'),
    path = require('path'),
    Q = require('q');

function download(url, filepath) {
  var fileStream = fs.createWriteStream(filepath),
      deferred = Q.defer();

  fileStream.on('open', function () {
    http.get(url, function (res) {
      res.on('error', function (err) {
        deferred.reject(err);
      });

      res.pipe(fileStream);
    });
  }).on('error', function (err) {
    deferred.reject(err);
  }).on('finish', function () {
    deferred.resolve(filepath);
  });

  return deferred.promise;
}

module.exports = {
  'download': download
};

Note I'm listening to finish on file stream instead of end on response.

fs.createWriteStream is creating file but not writing to it · Issue #1641 , not reproducible with v10.5 on mac. which platform do you use? $ cat 1641.js. const fs = require  The open/create only is synchronous, meaning it returns streams that are immediately available for read/write. I needed to replace stdio streams for child_process.spawnSync, but it requires the streams to have valid fd. fs.createXXXStream() returns immediately and fd is only valid after an open event, making it unsuitable for use with fully

Here is what I use to get it done:

function download(url, dest) {
    return new Promise((resolve, reject) => {
        http.get(url, (res) => {
            if (res.statusCode !== 200) {
                var err = new Error('File couldn\'t be retrieved');
                err.status = res.statusCode;
                return reject(err);
            }
            var chunks = [];
            res.setEncoding('binary');
            res.on('data', (chunk) => {
                chunks += chunk;
            }).on('end', () => {
                var stream = fs.createWriteStream(dest);
                stream.write(chunks, 'binary');
                stream.on('finish', () => {
                    resolve('File Saved !');
                });
                res.pipe(stream);
            })
        }).on('error', (e) => {
            console.log("Error: " + e);
            reject(e.message);
        });
    })
};

How to create a file using the filesystem (fs) module in Node.js, To create a file with Node.js, we are going to use the built-in There are 3 ways to create a file: asynchronously, synchronously and non-blocking: writeFileSync does, if the second parameter of createWriteStream is a string  A simple hello world example of fs.createWriteStream might involve just grabbing a reference to the file system module. Then use the fs.createWriteStream by calling it and passing the path to where the data should be saved as the first argument. Once that is done the write method can be used to write a string to the file.

I am working on uploading and downloading file (docx, pdf, text, etc) through nodejs request-promise and request libraries.

Problem with request-promise is that they don't promisify pipe method from request package. Hence, we need to do it in the old way.

I was able to come up with the hybrid solution, where I was able to use async/await and Promise() at same time. Here is the example:

    /**
     * Downloads the file.
     * @param {string} fileId : File id to be downloaded.
     * @param {string} downloadFileName : File name to be downloaded.
     * @param {string} downloadLocation : File location where it will be downloaded.
     * @param {number} version : [Optional] version of the file to be downloaded.
     * @returns {string}: Downloaded file's absolute path.
     */
    const getFile = async (fileId, downloadFileName, downloadLocation, version = undefined) => {
        try {
            const url = version ? `http://localhost:3000/files/${fileId}?version=${version}` : 
`${config.dms.url}/files/${fileUuid}`;
            const fileOutputPath = path.join(downloadLocation, fileName);

            const options = {
                method: 'GET',
                url: url,
                headers: {
                    'content-type': 'application/json',
                },
                resolveWithFullResponse: true
            }

            // Download the file and return the full downloaded file path.
            const downloadedFilePath = writeTheFileIntoDirectory(options, fileOutputPath);

            return downloadedFilePath;
        } catch (error) {
           console.log(error);
        }
    };

As you can see in above getFile method, we are using latest ES supported async/await functionality for asynchronous programming. Now, lets look into writeTheFileIntoDirectory method.

/**
 * Makes REST API request and writes the file to the location provided.
 * @param {object} options : Request option to make REST API request.
 * @param {string} fileOutputPath : Downloaded file's absolute path.
 */
const writeTheFileIntoDirectory = (options, fileOutputPath) => {
    return new Promise((resolve, reject) => {
        // Get file downloaded.
        const stream = fs.createWriteStream(fileOutputPath);
        return request
            .get(options.url, options, (err, res, body) => {
                if (res.statusCode < 200 || res.statusCode >= 400) {
                    const bodyObj = JSON.parse(body);
                    const error = bodyObj.error;
                    error.statusCode = res.statusCode;
                    return reject(error);
                }
            })
            .on('error', error => reject(error))
            .pipe(stream)
            .on('close', () => resolve(fileOutputPath));
    });
}

The beauty of nodejs is that it support backward compatibility of different asynchronous implementation. If a method is returning promise, then await will be kicked in and will wait for the method to be completed.

Above writeTheFileIntoDirectory method will download the file and will return positively when the stream is closed successfully, else it will return error.

How to use fs.createWriteStream?, createWriteStream() creates a writable stream in a very simple manner. file req.​pipe(writeStream); // After all the data is saved, respond with a  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Error: ENOENT, no such file or directory - fs.createReadStream that is piped into a fs.createWriteStream

fs.createWriteStream does not immediately create file?, I have made a simple download from http function as below (error handling is omitted for simplifcation): function download(url, tempFilepath, filepath, callback)​  fs.createWriteStream (filename) call is used to create a Write Stream and file with given filename. createStream.end () call ends or closes the opened stream. Before executing fs-create-file.js, first observe the filesystem project content and you will notice that “JournalDEV.txt” file is not available.

The fs.createWriteStream method, createWriteStream method in the node.js built in file system module, and why This method can be used to quickly make a writable stream for the This is not a getting started post on node.js, or javaScript in general, and I  Since I cannot comment yet, I'm posting an enhanced answer based on @tiago-peres-frança fantastic solution (thanks!). His code does not make directory in a case where only the last directory is missing in the path, e.g. the input is "C:/test/abc" and "C:/test" already exists.

Help with fs.createWriteStream, createWriteStream is bad practice because of race conditions, so I tried way to check if it already exists, and write to a new file if it does already exist? You can also use fs.access, which checks whehter or not the path is read / writable. We shall learn to create a File in Node.js using Node FS (File System) built-in module. Node.js example programs that use writeFile(), appendFile() or open() function are provided. Create a File in Node.js. Following is a step by step guide to create a new File in Node.js : Step 1: Include File System built-in module to your Node.js program

Comments
  • Somehow, this snippet of code didn't download some of the last bytes. Instead, I listen to finish on tempFile now and res.pipe(tempFile) instead of doing it manually.
  • The fs.js source indicates that there is no need to wait for the 'open' event before calling write on the write stream, as that is handled internally.
  • open is indeed handled internally, but it's async, the file is not open until the open event is emitted.