gpt4 book ai didi

node.js - fs.writeFileSync 在 Windows 上给出 ENOENT 错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:34 25 4
gpt4 key购买 nike

我有一个相当简单的调用来写入文件(如果需要,创建目录结构)。

writeFileSync(fileName, output, { flag: "w" });

在 Mac OS X 上,这可以按预期工作...但是,在 Windows 上,我收到 ENOENT 错误:

fs.js:549   
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);

Error: ENOENT: no such file or directory, open 'C:\mydir\myfile.json'
at Error (native)
at Object.fs.openSync (fs.js:549:18)
at Object.fs.writeFileSync (fs.js:1156:15)
at ...
at FSReqWrap.oncomplete (fs.js:82:15)

我真的很困惑,因为列出的文件路径是正确且有效的。而且,这在 Mac OS X 上的工作方式完全符合我的要求。

任何帮助将不胜感激。谢谢!

最佳答案

我愚蠢地使用了

mySimpleArrayOfStrings.join('/'); // not the way to build a path

而不是非常有用

path.join(...); // This is much better! Use this...

由于不使用此路径,我没有考虑 Windows 中的不同路径分隔符。

非常感谢@MykolaBorysyuk!

关于node.js - fs.writeFileSync 在 Windows 上给出 ENOENT 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38752265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com