带有标志“a”的nodejs fs.openSync()无法创build不存在的文件

我想为我的应用程序编写一个日志模块,它将根据模块初始化的时间(调用模块中的init函数)创build一个不存在的文件。

但是,当我尝试使用fs.openSync(log_file_name,'a')创build新的日志文件时fs.openSync(log_file_name,'a')它总是会出错。

我感谢任何告诉我为什么它不能创build新文件。

 const fs=require('fs'); const path=require('path'); const moment=require('moment'); var time=function() { return moment().format('YYYY-MM-DD[_]hh:mm:ss:SSS'); }; //init var fd; // file descriptor function init(log_dir) { var log_file_name=path.join(log_dir,time()+'.log'); this.fd=fs.openSync(log_file_name,'a'); } init(__dirname); 

错误如下所示:

  "C:\Program Files (x86)\JetBrains\PhpStorm 2016.1\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" index.js fs.js:634 return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode); ^ Error: ENOENT: no such file or directory, open 'I:\twesix\lib\twesix_nodejs\log\2016-05-19_01:34:52:621.log' at Error (native) at Object.fs.openSync (fs.js:634:18) at init (I:\twesix\lib\twesix_nodejs\log\index.js:15:16) at Object.<anonymous> (I:\twesix\lib\twesix_nodejs\log\index.js:24:1) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:456:32) at tryModuleLoad (module.js:415:12) at Function.Module._load (module.js:407:3) at Function.Module.runMain (module.js:575:10) Process finished with exit code 1 

我正在使用Windows 10和我的node.js版本是6.1.0

问题是由moment().format('YYYY-MM-DD[_]hh:mm:ss:SSS');返回的stringmoment().format('YYYY-MM-DD[_]hh:mm:ss:SSS'); 包含冒号(:),这些冒号在windows上的文件名是不允许的。

format()更改为不包含窗口文件名无效字符的内容,例如:

 return moment().format('YYYY-MM-DD[_]hh-mm-ss-SSS');