简体   繁体   English

节点:使用 fs.readFile 获取错误:EISDIR:对目录进行非法操作,读取

[英]Node: Using fs.readFile getting Error: EISDIR: illegal operation on a directory, read

I have a problem getting fs.readFile to return a css file.我在让fs.readFile返回一个 css 文件时遇到问题。 I've already read in this SO post that EISDIR error occurs when you try to open a file, but the path given is a directory.我已经在这篇 SO 文章中读到,当您尝试打开文件时会发生EISDIR error ,但给出的路径是一个目录。 But I've checked several times and the console.log inside the getCss function is logging the right path with the correct filename and .css extension.但是我已经检查了几次, getCss函数中的console.log正在使用正确的文件名和.css扩展名记录正确的路径。 What's the issue?有什么问题? Thanks in advance!提前致谢!

// Path to MUI css
muiCssFile = path.join(muiDistFolder, 'css', 'mui.css'),  


// Read css files
function getCss(callback) {
  console.log('MUI CSS file path in getCss() :: ', muiCssFile);
  // Code fails here.... 
  fs.readFile(muiCssFile, 'utf8', function (err, css) {
    if (err) {
      return callback(err);
    }
    fs.readFile(styleFile, function (error, customCss) {
      if (error) return callback(error);
      callback(null, css + customCss);
    });
  });

} }

Ok, just for anyone stumbling across this error I thought it would help to provide a short answer to my question.好的,对于任何遇到此错误的人,我认为对我的问题提供简短的答案会有所帮助。 The error is most likely caused by one or more of the paths you are trying to use fs.readFile on ending on a directory path and NOT a file.该错误很可能是由您尝试使用fs.readFile以目录路径而不是文件结尾的一个或多个路径引起的。

In my specific case above, the error was actually occurring on the second fs.readFile call so be sure to check ALL paths first and ensure that they do in fact lead to files to uncover the problem path.在我上面的特定情况下,错误实际上发生在第二次fs.readFile调用中,因此请务必先检查所有路径并确保它们确实指向文件以发现问题路径。 Hope that helps someone save some time getting past this error.希望能帮助某人节省一些时间来克服这个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM