Skip to main content

read-pkg

A lightweight utility to read and parse package.json files synchronously.

Installation

npm install @mapcolonies/read-pkg

Usage

import { readPackageJsonSync } from '@map-colonies/read-pkg';

// Read package.json from current directory
const packageJson = readPackageJsonSync();
console.log(packageJson.name);

// Read package.json from specific path
const otherPackageJson = readPackageJsonSync('./path/to/package.json');
console.log(otherPackageJson.version);

API

Check the autogenerated documentation here.

readPackageJsonSync(path?: string, encoding?: BufferEncoding): PackageJson

Synchronously reads and parses a package.json file.

Parameters

  • path (optional): Path to the package.json file. Defaults to './package.json'
  • encoding (optional): File encoding to use. Defaults to 'utf-8'

Returns

Returns a PackageJson object from the type-fest package.

Throws

  • SyntaxError: If the JSON content is invalid
  • Error: If the file cannot be read or does not exist
note

This page was generated from a remote source. you can find it on https://github.com/MapColonies/read-pkg/blob/master/README.md