[英]Writing to ~/Documents folder in Node.js on macOS?
In Node.js, I am attempting to write into the Documents
folder of the user (on macOS): 在Node.js中,我试图写入用户的
Documents
文件夹(在macOS上):
var logger = fs.createWriteStream('~/Documents/somefolderwhichexists/'+title+'.txt');
This gives me an error, but I am not sure what is wrong. 这给了我一个错误,但我不确定是什么问题。 The error says:
错误说:
Uncaught Exception: Error: ENOENT: no such file or directory
未捕获的异常:错误:ENOENT:没有这样的文件或目录
How could I use an absolute path here? 我怎么能在这里使用绝对路径? Or what is my mistake?
或者我的错误是什么?
The ~
is a shorthand, to your home directory , expanded by the Bash shell. ~
是您的主目录的简写,由Bash shell扩展。 You need to use a fully defined path or get the current user name (or home path) dynamically. 您需要使用完全定义的路径或动态获取当前用户名(或主路径)。
To get the current user home path, ty this: 要获取当前用户的主路径,请执行以下操作:
var home = require("os").homedir();
var logpath = home + '/Documents/somefolderwhichexists/' + title + '.txt';
var logger = fs.createWriteStream(logpath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.