Access the home directory in Deno

If you want to access the home directory from you Deno program, you can use the Deno.env's get method with "HOME":

Deno.env.get("HOME")

In unix environments, returns a string along the lines of "/home/USER".

So if you want to write a script that accesses a configuration file like .bashrc, you could read the file with:

// read-bashrc.ts
const bashConfig = Deno.readTextFileSync(
Deno.env.get("HOME") + "/.bashrc"
);

Since we are accessing Deno.env, we need to run the program with the env permission:

deno run --allow-env read-bashrc.ts

Here is a Repl where you can play around with accessing the home directory in Deno.

Happy coding!

My name is Sean McPherson (SeanMcP), and I am a software engineer and educator in Pittsburgh, PA. I have given conference talks, lead workshops, and organized meetups. Here I write articles about programming for developers of all levels.