DEV Community

Cover image for How to get the current home directory of the user in Node.js?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to get the current home directory of the user in Node.js?

Originally posted here!

To get the current user home directory, you can use the homedir() method from the os module in Node.js.

/* Get home directory of the user in Node.js */

// import os module
const os = require("os");

// check the available memory
const userHomeDir = os.homedir();
Enter fullscreen mode Exit fullscreen mode
  • The method returns the path to the directory as a string.

See the above code live in repl.it.

Feel free to share if you found this useful 😃.


Top comments (3)

Collapse
 
aschmelyun profile image
Andrew Schmelyun • Edited

You can also use the method directly with esm:

import { homedir } from 'os'

const userHomeDir = homedir()
Enter fullscreen mode Exit fullscreen mode
Collapse
 
melvin2016 profile image
MELVIN GEORGE

Thanks for sharing it, Andrew.

Collapse
 
shareef profile image
Mohammed Nadeem Shareef

It really helped me. Thanks man