![](/img/trans.png)
[英]How can i add the functionality in my web App So that my Application user can create their Paypal Account using API in Nodejs?
[英]How can I create a directory in user home using nodejs?
我正在 Ubuntu 上開發 node.js 應用程序,並嘗試以編程方式在用戶的主目錄中為我的應用程序創建一個目錄。
當我在 Node 中執行以下 Javascript 時:
const fs = require("fs");
fs.mkdirSync("~/mynewdir");
我收到以下錯誤:
Error: ENOENT: no such file or directory, mkdir '~/mynewdir'
at Error (native)
at Object.fs.mkdirSync (fs.js:923:18)
at repl:1:4
at sigintHandlersWrap (vm.js:22:35)
at sigintHandlersWrap (vm.js:73:12)
at ContextifyScript.Script.runInThisContext (vm.js:21:12)
at REPLServer.defaultEval (repl.js:346:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:545:10)
執行:
fs.mkdirSync("/home/dah/mynewdir");
然而,工作得很好,但我想使用執行腳本的人的主目錄。
有沒有人有什么建議?
編輯 - 這個問題不是重復的。 在這種情況下,問題不是找到主目錄(我已經有了這個),而是為什么 fs 模塊不會使用它。
你可以這樣做:
const homedir = require('os').homedir();
// `homedir()` returns absolute path so we use `join` here
require("fs").mkdir(require('path').join(homedir, 'mynewdir'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.