Skip to content

Latest commit

 

History

History
42 lines (28 loc) · 1.04 KB

File metadata and controls

42 lines (28 loc) · 1.04 KB
description Native Node.js alternatives to the read-pkg package for reading package.json files

Replacements for read-pkg

pkg-types

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 })

Native node:fs

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).