Socket
Socket
Sign inDemoInstall

read-pkg

Package Overview
Dependencies
32
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

read-pkg

Read a package.json file


Version published
Maintainers
1
Weekly downloads
34,543,164
decreased by-8.76%

Weekly downloads

Package description

What is read-pkg?

The read-pkg npm package is used to read a package.json file and parse its contents. It can normalize the data according to the npm normalization rules, handle different file encodings, and can read from different locations by resolving the path to the nearest package.json file.

What are read-pkg's main functionalities?

Read package.json from the current directory

This feature reads the package.json file from the current working directory and outputs the parsed JSON object.

const readPkg = require('read-pkg');

(async () => {
  const pkg = await readPkg();
  console.log(pkg);
})();

Read package.json from a specified directory

This feature allows you to specify a directory from which to read the package.json file, rather than the current working directory.

const readPkg = require('read-pkg');

(async () => {
  const pkg = await readPkg({ cwd: 'path/to/directory' });
  console.log(pkg);
})();

Read and normalize package.json data

This feature reads the package.json file and normalizes its data according to npm normalization rules, which can be useful for ensuring consistent data structure.

const readPkg = require('read-pkg');

(async () => {
  const pkg = await readPkg({ normalize: true });
  console.log(pkg);
})();

Other packages similar to read-pkg

Readme

Source

read-pkg

Read a package.json file

Why

  • Throws more helpful JSON errors
  • Normalizes the data

Install

npm install read-pkg

Usage

import {readPackage} from 'read-pkg';

console.log(await readPackage());
//=> {name: 'read-pkg', …}

console.log(await readPackage({cwd: 'some-other-directory'}));
//=> {name: 'unicorn', …}

API

readPackage(options?)

Returns a Promise<object> with the parsed JSON.

readPackageSync(options?)

Returns the parsed JSON.

options

Type: object

cwd

Type: URL | string
Default: process.cwd()

Current working directory.

normalize

Type: boolean
Default: true

Normalize the package data.

parsePackage(packageFile, options?)

Parses an object or string into JSON.

Note: packageFile is cloned using structuredClone to prevent modification to the input object. This function is available from Node.js 18 on. In environments without structuredClone (such as Node.js 16), a shallow spread is used instead, which can cause deep properties of the object to be modified. Consider cloning the object before using parsePackage if that's the case.

packageFile

Type: object | string\

An object or a stringified object to be parsed as a package.json.

options

Type: object

normalize

Type: boolean
Default: true

Normalize the package data.

Keywords

FAQs

Last updated on 07 Apr 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc