27
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.jsでユーザーのホームディレクトリのパスを取得する

Last updated at Posted at 2016-08-24

Node.jsでホームディレクトリを取得したい場合、特にmoduleのrequireは必要とせず、以下のコードで取得が可能。

main.js
console.log(process.env[process.platform == "win32" ? "USERPROFILE" : "HOME"]);
// /Users/foo/

デスクトップやDocumentsを指定したい場合はpathを利用して。

main.js
const path = require("path");
const userHome = process.env[process.platform == "win32" ? "USERPROFILE" : "HOME"];

console.log(path.join(userHome, "Desktop"));
// /Users/foo/Desktop

console.log(path.join(userHome, "Documents"));
// /Users/foo/Documents
27
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?