Replacements for read-pkg-up
pkg-types
pkg-types
provides a similar API and strong types.
For example:
ts
import { readPackageJSON } from 'pkg-types'
import { readPackageUp } from 'read-pkg-up'
const packageJson = await readPackageUp()
const packageJson = await readPackageJSON()
Similarly, you can get hold of the path via resolvePackageJSON
:
ts
import { resolvePackageJSON } from 'pkg-types'
const packageJsonPath = await resolvePackageJSON()
empathic
empathic
provides a more generic way to find files and directories upwards.
It can be combined with node:fs
to read package.json
files:
ts
import fs from 'node:fs/promises'
import * as pkg from 'empathic'
import { readPackageUp } from 'read-pkg-up'
const packageJson = await readPackageUp()
const packageJsonPath = pkg.up()
const packageJson = packageJsonPath ? JSON.parse(await readFile(packageJsonPath, 'utf8')) : undefined
NOTE
This is of course a more manual way to read the package.json
file, so one of the other options may be more attractive.