snippetjavascriptTip
Convert a tilde path to an absolute path in Node.js
Viewed 0 times
javascriptpathnodetildeconvertabsolute
Problem
Have you ever wanted to convert a tilde path to an absolute path in JavaScript? Maybe you've used the
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
Having matched the tilde path, you can then use
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.Solution
import { homedir } from 'os';
const untildify = str => str.replace(/^~($|\/|\\)/, `${homedir()}$1`);
untildify('~/node'); // '/Users/aUser/node'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.Code Snippets
import { homedir } from 'os';
const untildify = str => str.replace(/^~($|\/|\\)/, `${homedir()}$1`);
untildify('~/node'); // '/Users/aUser/node'Context
From 30-seconds-of-code: convert-to-absolute-path
Revisions (0)
No revisions yet.