本文介绍了Windows文件系统路径中的Node.js错误4058 ENOENT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 windows 中将文件系统与nodejs 一起使用编写过程日志.我有以下代码

I am using file system with nodejs in windows to write process logs. I have follwing code

var fs = require('fs');
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var statusLogStream = fs.createWriteStream("../logs/load stat"+(new Date())+".log");

结果是错误

    { [Error: ENOENT: no such file or directory, open 'C:\proc\logs\load stat Mon Apr 18 2016 19:09:32 GMT+0530 (India Standard Time).log']
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\proc\\logs\\load stat Mon Apr 18 2016 19:09:32 GMT+0530 (India Standard Time).log' }
events.js:141
      throw er; // Unhandled 'error' event
  ^

我尝试通过文件夹手动打开文件, C:\\ proc \\ logs 无效,替换后的 C:/proc/logs 我可以手动从资源管理器中打开文件夹.

I try the folder to open the file manually C:\\proc\\logs it doesn't work and C:/proc/logs this when I replace double backward slash by forward slash I can manually open the folder from explorer.

如何使其正常工作

为什么要使用双反斜杠而不是正斜杠

IMP:上面的代码在 linux ubuntu 服务器上工作正常,但在 windows

IMP: The above code works perfectly fine in linux ubuntu server but not in windows

推荐答案

问题不在于斜杠,而在于日期如何转换为字符串.

The problem is not about the slashes but on how the date is converted to string.

我敢打赌这会起作用:

var statusLogStream = fs.createWriteStream("../logs/load stat.log");

更新Windows抱怨日期的字符串表示形式中的两个冒号( Mon Apr 18 2016 19 **:** 09 **:** 32 GMT + 0530(印度标准时间))

UpdateWindows is complaining about the two colons in the string representation of date (Mon Apr 18 2016 19**:**09**:**32 GMT+0530 (India Standard Time))

这可能是一个很好的选择:

This could be a good alternative:

var myDate = new Date().toJSON().replace(new RegExp(':', 'g'),'.');
// myDate is now "2016-04-18T15.19.21.174Z"
var statusLogStream = fs.createWriteStream("../logs/load stat"+(myDate)+".log");

这篇关于Windows文件系统路径中的Node.js错误4058 ENOENT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:01