【问题标题】:ENOENT: no such file or directory writing file with fsENOENT:没有这样的文件或目录使用 fs 写入文件
【发布时间】:2016-10-06 07:51:15
【问题描述】:

我不确定我哪里出错了:

{ 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);
    });
});

如您所见,我将dist 目录放在正确的位置,并且生成并构建了从webpack 生成的其他文件

项目结构:

【问题讨论】:

  • 在需要“路径”后尝试使用 path.resolve()

标签: javascript node.js express ecmascript-6


【解决方案1】:

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

fs.writeFile(__dirname + '/../dist/index.html', $.html(), 'utf8', err => {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-03
    • 2014-01-12
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2013-07-28
    • 2019-02-03
    • 1970-01-01
    相关资源
    最近更新 更多