Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ENOENT: no such file or directory writing file with fs

I am not sure where I went wrong to get this error:

{ Error: ENOENT: no such file or directory, open '../dist/index.html'
    at Error (native)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '../dist/index.html' }

I put a console.log("Build HTML error") in my code to see if the error fires and it does. I also used console.log($.html()); to see if the correct HTML is generated and it is.

my code:

fs.readFile(__dirname + '/../app/index.html', 'utf8', (err, markup) => {
    if (err) {
        console.log("grab HTML error");
        return console.log(err);
    }

    const $ = cheerio.load(markup);

    console.log($.html());
    $('head').prepend('<link rel="stylesheet" href="styles.css">');

    fs.writeFile('../dist/index.html', $.html(), 'utf8', err => {
       if (err) {
           console.log("Build HTML error");
           return console.log(err);
       }
       console.log('index.html written to /dist'.green);
    });
});

As you can see, I have the dist directory in the correct place and the other files generated from webpack are generated and built

Project structure:

enter image description here

like image 900
Liondancer Avatar asked Oct 22 '25 04:10

Liondancer


1 Answers

I'm fairly certain it has to do with your relative path. You should use the same path strategy you used for readFile:

fs.writeFile(__dirname + '/../dist/index.html', $.html(), 'utf8', err => {
like image 168
Davin Tryon Avatar answered Oct 24 '25 17:10

Davin Tryon