| description | Native Node.js alternatives to the read-pkg package for reading package.json files |
|---|
pkg-types provides a similar API and strong types.
For example:
import { readPackageJSON } from 'pkg-types' // [!code ++]
import { readPackage } from 'read-pkg' // [!code --]
const packageJson = await readPackage() // [!code --]
const packageJson = await readPackageJSON() // [!code ++]You may also specify a cwd:
import { readPackageJSON } from 'pkg-types'
const packageJson = await readPackageJson({ cwd })You can use node:fs to read a known package.json:
import fs from 'node:fs/promises' // [!code ++]
import { readPackage } from 'read-pkg' // [!code --]
const packageJson = await readPackageUp() // [!code --]
const packageJson = JSON.parse(await readFile('./package.json', 'utf8')) // [!code ++]Note
Using this approach, you will have to handle errors yourself (e.g. failure to read the file).