1

I'm trying to write to a file using the following function:

function writeFile (data, callback) {
var fs = require('fs');
var now = new Date();

fs.writeFile(now.toISOString() + ".json", data, function(err) {

    if (err) {
        return console.log(err);
    } else {
        console.log(true);
    }
});
}

but im getting an error like this:

{ Error: ENOENT: no such file or directory, open 'C:\Users\Ruslan\WebstormProjects\communication-system\client\6\28\2017_19:47:55.json'
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\Me\\WebstormProjects\\blah-blah\\client\\6\\28\\2017_19:47:55.json' }

I'm trying to create a file every time I run the program, but that doesn't seem to work very well because it says file does not exist. Is there anything im doing wrong? BTW, im running this on windows

EDIT: It was indeed wrong file name that was bugging the saving process

1 Answers1

4

When you call fs.writeFile() you have to pass it a filename/path:

  1. Where the parent directory in the path already exists.
  2. Where the path/filename contains only characters that are legal for your OS.

It appears you are likely failing both of these unless you've pre-created the directory: C:\Users\Ruslan\WebstormProjects\communication-system\client\6\28. And, if this is running on Windows, then you also can't use : in a filename.

Assume you actually want the path to be C:\Users\Ruslan\WebstormProjects\communication-system\client and what the filename to be based on your now.toISOString(), the usual work-around is to replace path separators and other invalid filename characters with safe characters to you convert your now.toISOString() to something that is always a safe filename. In this case, you could do this:

// replace forward and back slashes and colons with an underscore
// to make sure this is a legal OS filename
let filename = now.toISOString().replace(/[\/\\:]/g, "_") + ".json";

fs.writeFile(filename, ....)
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Added the `g` flag to the regex as that is needed to get all the inappropriate characters. – jfriend00 Jun 29 '17 at 02:22
  • Updating this. fs.WriteFile doesn't seem to throw an error any more if given an invalid file name (character wise) on Windows. Rather it creates an empty file with a name equal to all the characters up until the first invalid character. ie: "test: my test.json" will get written as just "test" – madmonkey Jan 15 '23 at 01:15
  • @madmonkey - Whether it throws or truncates the name will depend upon exactly what filename you give it and exactly which OS you're running on. To get predictable behavior, it's really your responsibility as a developer to make sure you only create legal filenames. That would mean sanitizing any user-generated naming to remove undesirable characters and would also include making sure there's no ..\ or ../ in it either that lets the filename go elsewhere from where it's supposed to be. – jfriend00 Jan 15 '23 at 08:47
  • sorry, it’s only the colon actually that doesn’t throw an error. But yes, I should be sanitizing the file names regardless. – madmonkey Jan 17 '23 at 03:18