[英]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. 我在代码中放入了
console.log("Build HTML error")
,以查看该错误是否触发并发生。 I also used console.log($.html());
我还使用了
console.log($.html());
to see if the correct HTML is generated and it is. 查看是否生成了正确的HTML。
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 如您所见,我在正确的位置放置了
dist
目录,并且生成并构建了从webpack
生成的其他文件
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
: 您应该使用与
readFile
相同的路径策略:
fs.writeFile(__dirname + '/../dist/index.html', $.html(), 'utf8', err => {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.