Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/modules/cwd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
description: Modern alternatives to the cwd package for resolving project-relative paths
---

# Replacements for `cwd`

## `empathic`

[`empathic`](https://github.com/lukeed/empathic) provides a more generic way to find files and directories upwards.

[`cwd`](https://github.com/jonschlinkert/cwd) is not `process.cwd()`. It finds the nearest `package.json` and returns an absolute path anchored to that package root. Unlike [`pkg-dir`](./pkg-dir.md), 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' // [!code ++]
import * as pkg from 'empathic/package' // [!code ++]
import cwd from 'cwd' // [!code --]

const filepath = resolve('one/two.js') // [!code ++]
const pkgPath = pkg.up({ cwd: filepath }) // [!code ++]
const result = cwd('one/two.js') // [!code --]
const result = pkgPath ? resolve(dirname(pkgPath), filepath) : filepath // [!code ++]
```
6 changes: 6 additions & 0 deletions manifests/preferred.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@
"replacements": ["node:crypto", "crypto", "Bun.CryptoHasher"],
"url": {"type": "e18e", "id": "crypto-js"}
},
"cwd": {
"type": "module",
"moduleName": "cwd",
"replacements": ["empathic"],
"url": {"type": "e18e", "id": "cwd"}
},
"debug": {
"type": "module",
"moduleName": "debug",
Expand Down