Skip to content

Replacements for cwd

empathic

empathic provides a more generic way to find files and directories upwards.

cwd is not process.cwd(). It finds the nearest package.json and returns an absolute path anchored to that package root. Unlike pkg-dir, which returns the package root directory, cwd returns a resolved path relative to that root.

The main difference is that empathic is synchronous, so you should no longer await the result.

Example:

ts
import { resolve, dirname } from 'node:path'
import * as pkg from 'empathic/package'
import cwd from 'cwd'

const filepath = resolve('one/two.js') 
const pkgPath = pkg.up({ cwd: filepath }) 
const result = cwd('one/two.js') 
const result = pkgPath ? resolve(dirname(pkgPath), filepath) : filepath 

Released under the MIT License. (aba2dbd5)