在 Node.js 中,我试图写入用户的 Documents
文件夹(在 macOS 上):
var logger = fs.createWriteStream('~/Documents/somefolderwhichexists/'+title+'.txt');
这给了我一个错误,但我不确定哪里出了问题。错误说:
Uncaught Exception: Error: ENOENT: no such file or directory
我怎么能在这里使用绝对路径呢?或者我的错误是什么?
最佳答案
~
是您的主目录 的简写,由 Bash shell 扩展。您需要使用完全定义的路径或动态获取当前用户名(或主路径)。
要获取当前用户的主路径,请键入:
var home = require("os").homedir();
var logpath = home + '/Documents/somefolderwhichexists/' + title + '.txt';
var logger = fs.createWriteStream(logpath);
关于javascript - 在 macOS 上写入 Node.js 中的 ~/Documents 文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43858683/