Cross-platform ESM path helpers.
Because Node.js ESM path handling on Windows is messy with POSIX.
npm i add -E esm-path
or:
yarn add -E esm-path
import { getAbsolutePath } from 'esm-path'
getAbsolutePath(importMetaUrl: string, ...relativePaths: string[]): string
importMetaUrl
: must always be import.meta.url
....relativePaths
: list of paths, relative to the directory or file from which this function is called.Return the absolute path of the targetted directory or file.
import { getAbsolutePath } from 'esm-path'
const currentDirectoryPath = getAbsolutePath(import.meta.url)
console.log(currentDirectoryPath)
const parentDirectoryPath = getAbsolutePath(import.meta.url, '..')
console.log(parentDirectoryPath)
// Adapt the relative path to your case
const packageJsonFilePath = getAbsolutePath(import.meta.url, '../package.json')
console.log(packageJsonFilePath)
// Adapt the relative path to your case
const packageJsonFilePath = getAbsolutePath(import.meta.url, '..' , 'package.json')
console.log(packageJsonFilePath)