简体   繁体   English

ENOENT:没有这样的文件或目录使用fs写入文件

[英]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.

相关问题 错误:ENOENT:没有这样的文件或目录,打开尝试使用 fs 访问目录时 - Error: ENOENT: no such file or directory, open When trying to access a directory with fs fs.createReadStream 错误; uncaughtException: ENOENT: 没有这样的文件或目录 - fs.createReadStream error; uncaughtException: ENOENT: no such file or directory ENOENT:运行fs.readFile时没有此类文件或目录 - ENOENT: no such file or directory when running fs.readFile ENOENT:没有此类文件或目录,请取消链接 - ENOENT: no such file or directory, unlink 错误:ENOENT:没有这样的文件或目录 - Error: ENOENT: no such file or directory ENOENT:没有这样的文件或目录Nodejs - ENOENT: no such file or directory Nodejs ENOENT:盖茨比中没有这样的文件或目录 - ENOENT: no such file or directory in Gatsby ENOENT:没有这样的文件或目录 - ENOENT: no such file or directory 节点js-fs.unlink仅删除forEach序列中的第一个文件,然后抛出ENOENT没有此类文件或目录 - node js - fs.unlink only deletes first file in a forEach sequence then throw ENOENT no such file or directory 我正在使用 fs 模块 readdir 但我不断收到此错误(错误:ENOENT:没有这样的文件或目录,scandir) - I am using fs module readdir but I keep getting this error (Error: ENOENT: no such file or directory, scandir)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM