Provides a helper function to expand a path to a normalized absolute path. This function also expands dot, tilde, and tilde plus when used as the first path segment.


2.0.0a year ago4 years agoMinified + gzip package size for @antora/expand-path-helper in KB



A Node.js module that provides a helper function to expand a path to a normalized absolute path. This function also expands the following shorthand expressions when used as the first path segment: dot (.), tilde (~), or tilde plus (~+). The expanded path is system dependent. Developed for use in Antora.


$ npm i @antora/expand-path-helper


$ yarn add @antora/expand-path-helper


function expandPath (path, { base = '~+', cwd, dot = '.' } = {}) { ... }

Expands the specified path to a normalized absolute path.

  • path <string> - The path to expand. This parameter is required. If the path is already absolute, the path is normalized and returned.
  • context <Object> - Named parameters that control how the path is resolved. All named parameters are optional. This parameter can also be specified as a String, in which case it is used as the base argument.
  • context.base <string> - The base directory from which to resolve a relative path instead of the working directory. This named parameter is optional.
  • context.cwd <string> - The absolute directory to use as the working directory instead of the current working directory. This named parameter is optional.
  • context.dot - The value to use to replace a leading dot (.) segment. This named parameter is optional. If the dot argument is ., which is the default value, the base argument is used instead.

The main purpose of this function is path expansion and normalization. If the first segment of the path argument, the base argument, or the dot argument is ~ or ~+, that value is expanded to the user's home directory or current working directory, respectively. If the first segment of the path argument is ., that value is replaced with the dot argument after the dot argument is expanded.


The output of expandPath depends on the system on which it is run (specifically on the path.sep value).


const expandPath = require('@antora/expand-path-helper')

//=> /absolute/path

//=> /absolute

//=> $PWD/foo/bar

//=> $PWD/foo/bar

//=> $HOME/foo/bar

//=> $PWD/foo/bar

expandPath('~+/foo/bar', { cwd: '/working/dir' })
//=> /working/dir/foo/bar

expandPath('foo/bar', '/base/dir')
//=> /base/dir/foo/bar

expandPath('foo/bar', { base: '/base/dir' })
//=> /base/dir/foo/bar

expandPath('./foo/bar', { base: '/base/dir' })
//=> /base/dir/foo/bar

expandPath('./foo/bar', { dot: '/dot/dir' })
//=> /dot/dir/foo/bar


const expandPath = require('@antora/expand-path-helper')

//=> C:\absolute\path

//=> C:\absolute

//=> C:\current\directory\foo\bar

//=> C:\current\directory\foo\bar

//=> C:\Users\user\foo\bar

//=> C:\current\directory\foo\bar

expandPath('~+\\foo\\bar', { cwd: 'C:\\working\\dir' })
//=> C:\working\dir\foo\bar

expandPath('foo\\bar', 'C:\\base\\dir')
//=> C:\base\dir\foo\bar

expandPath('foo\\bar', { base: 'C:\\base\\dir' })
//=> C:\base\dir\foo\bar

expandPath('.\\foo\\bar', { base: 'C:\\base\\dir' })
//=> C:\base\dir\foo\bar

expandPath('.\\foo\\bar', { dot: 'C:\\dot\\dir' })
//=> C:\dot\dir\foo\bar

On Windows, the input path may use forward slashes, but the expanded path will always have backslashes.

Copyright and License

Copyright (C) 2018-present by OpenDevise Inc. and the individual contributors to Antora.

Use of this software is granted under the terms of the Mozilla Public License Version 2.0 (MPL-2.0).

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.