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:

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 => {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With