Back to 30 Seconds Of Code

Convert a tilde path to an absolute path in Node.js

content/snippets/js/s/convert-to-absolute-path.md

14.0.0912 B
Original Source

Have you ever wanted to convert a tilde path to an absolute path in JavaScript? Maybe you've used the untildify package, but you want to do it without a dependency. Or you're just wondering how it works under the hood.

As with many other things in JavaScript, the answer is to simply use a regular expression. The expression that's often used to match a tilde path is ^~($|\/|\\). This expression matches a tilde (~) at the start of the string, followed by either the end of the string, a forward slash, or a backslash.

Having matched the tilde path, you can then use os.homedir() to get the home directory and String.prototype.replace() to replace the tilde with the home directory.

js
import { homedir } from 'os';

const untildify = str => str.replace(/^~($|\/|\\)/, `${homedir()}$1`);

untildify('~/node'); // '/Users/aUser/node'