windows 文件系统路径中的 nodejs 错误 4058 ENOENT
nodejs in windows file system path error 4058 ENOENT
我正在 windows
中使用 file system with nodejs 来写入进程日志。我有以下代码
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
当我用正斜杠替换双反斜杠时,我可以从资源管理器手动打开文件夹。
如何让它发挥作用
为什么要 double backward slash
而不是 forward slash
IMP:以上代码在 linux ubuntu
服务器中运行良好,但在 windows
中运行良好
问题不在于斜杠,而在于如何将日期转换为字符串。
我打赌这会奏效:
var statusLogStream = fs.createWriteStream("../logs/load stat.log");
更新
Windows 抱怨日期字符串表示中的两个冒号 (Mon Apr 18 2016 19**:**09**:**32 GMT+0530 (India Standard Time)
)
这可能是一个不错的选择:
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");
我遇到了同样的问题。
在我的项目中,当我 运行 一个快速应用程序时,我注意到当前工作目录是项目的根目录(当我试图读取位于脚本目录中的文件时)。
它无法 运行 文件,因为 process.cwd() !== __dirname
。
您可以在尝试读取 json 文件的脚本中查看并控制台记录 process.cwd()
。
我刚刚将路径更改为:
const fs = require('fs');
fs.readFileSync(`${__dirname}\FILENAME`);
我正在 windows
中使用 file system with nodejs 来写入进程日志。我有以下代码
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
当我用正斜杠替换双反斜杠时,我可以从资源管理器手动打开文件夹。
如何让它发挥作用
为什么要 double backward slash
而不是 forward slash
IMP:以上代码在 linux ubuntu
服务器中运行良好,但在 windows
问题不在于斜杠,而在于如何将日期转换为字符串。
我打赌这会奏效:
var statusLogStream = fs.createWriteStream("../logs/load stat.log");
更新
Windows 抱怨日期字符串表示中的两个冒号 (Mon Apr 18 2016 19**:**09**:**32 GMT+0530 (India Standard Time)
)
这可能是一个不错的选择:
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");
我遇到了同样的问题。
在我的项目中,当我 运行 一个快速应用程序时,我注意到当前工作目录是项目的根目录(当我试图读取位于脚本目录中的文件时)。
它无法 运行 文件,因为 process.cwd() !== __dirname
。
您可以在尝试读取 json 文件的脚本中查看并控制台记录 process.cwd()
。
我刚刚将路径更改为:
const fs = require('fs');
fs.readFileSync(`${__dirname}\FILENAME`);