Convert to absolute path

JavaScript, Node.js, String · Oct 22, 2020

Converts a tilde path to an absolute path.

  • Use String.prototype.replace() with a regular expression and os.homedir() to replace the ~ in the start of the path with the home directory.
import { homedir } from 'os';

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

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

More like this

  • JavaScript String Snippets

    Master string manipulation in JavaScript with this ES6 snippet collection.

    Collection · 107 snippets

  • Encode string to Base64

    Creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

    JavaScript, Node.js · Sep 15, 2020

  • Introduction to the Node.js test module

    The Node.js test module is a new testing tool that's still in its early stages. Learn more about it in this short introduction.

    JavaScript, Node.js · Apr 30, 2023

  • Introduction to Semantic Versioning (SemVer)

    Learn how semantic versioning works and how to use it to correctly version your software.

    JavaScript, Node.js · Jul 16, 2023