最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Writing to ~Documents folder in Node.js on macOS? - Stack Overflow

programmeradmin3浏览0评论

In Node.js, I am attempting to write into the Documents folder of the user (on 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

How could I use an absolute path here? Or what is my mistake?

In Node.js, I am attempting to write into the Documents folder of the user (on 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

How could I use an absolute path here? Or what is my mistake?

Share Improve this question edited Dec 26, 2017 at 14:29 Paulo Mattos 19.3k10 gold badges79 silver badges86 bronze badges asked May 8, 2017 at 23:22 George WelderGeorge Welder 4,05511 gold badges44 silver badges75 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 17

The ~ is a shorthand, to your home directory, expanded by the 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);
发布评论

评论列表(0)

  1. 暂无评论