Javascript eNONT:没有这样的文件或目录写入文件与fs

Javascript eNONT:没有这样的文件或目录写入文件与fs,javascript,node.js,express,ecmascript-6,Javascript,Node.js,Express,Ecmascript 6,我不确定是哪里出了错,导致了这个错误: { Error: ENOENT: no such file or directory, open '../dist/index.html' at Error (native) errno: -2, code: 'ENOENT', syscall: 'open', path: '../dist/index.html' } 我在代码中放了一个console.log(“Build-HTML-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' }
我在代码中放了一个
console.log(“Build-HTML-error”)
,看看是否触发了错误,它是否触发了错误。我还使用了
console.log($.html())
以查看是否生成了正确的HTML,并且它是正确的

我的代码:

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);
    });
});
fs.readFile(uu dirname+'/../app/index.html',utf8',(err,markup)=>{
如果(错误){
log(“抓取HTML错误”);
返回console.log(err);
}
const$=cheerio.load(标记);
log($.html());
$('head')。前置('');
fs.writeFile('../dist/index.html',$.html(),'utf8',err=>{
如果(错误){
log(“生成HTML错误”);
返回console.log(err);
}
log('index.html写入/dist'.green);
});
});
如您所见,我将
dist
目录放在正确的位置,并生成和构建从
webpack
生成的其他文件

项目结构:


我很确定这与你的相对路径有关。您应该使用与
readFile
相同的路径策略:

fs.writeFile(__dirname + '/../dist/index.html', $.html(), 'utf8', err => {
尝试在要求“ing”path之后使用path.resolve()