我有:
fs.readFile('../services/Prescipcion.xml', "utf8", function (err, data) {
console.log("err->", err);
console.log("data", data);
});
它会记录:
err-> {
[Error: ENOENT: no such file or directory, open '../services/Prescipcion.xml']
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '../services/Prescipcion.xml'
}
我不明白为什么会这样。
最佳答案
错误信息是no such file or directory
,所以乍一看这很可能意味着文件的路径不正确。
文件名不正确(错字?)或目录解析不正确。请注意,将针对 process.cwd()
解析相对路径:
process.cwd()
: Returns the current working directory of the process.
您可以尝试使用 console.log(process.cwd())
来帮助您调试问题。
如果文件 Prescipcion.xml
应该从运行脚本的地方本地检索,您还可以使用以下构造:
fs.readFileSync(path.join(__dirname, '../services') + '/Prescipcion.xml', 'utf8');
__dirname
: The name of the directory that the currently executing script resides in.
关于node.js - 不适用于 Node js 中的 fs.readFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33342984/