🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

@npmcli/package-json

Package Overview
Dependencies
Maintainers
6
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@npmcli/package-json

Programmatic API to update package.json

6.2.0
latest
Source
npm
Version published
Weekly downloads
6.8M
2.45%
Maintainers
6
Weekly downloads
 
Created

What is @npmcli/package-json?

@npmcli/package-json is a Node.js library that provides utilities for reading, manipulating, and writing package.json files. It simplifies the process of handling package.json files programmatically, making it easier to manage dependencies, scripts, and other metadata in a Node.js project.

What are @npmcli/package-json's main functionalities?

Read package.json

This feature allows you to read the contents of a package.json file. The code sample demonstrates how to load the package.json file from the current working directory and log its contents.

const { PackageJson } = require('@npmcli/package-json');

async function readPackageJson() {
  const pkg = await PackageJson.load(process.cwd());
  console.log(pkg.content);
}

readPackageJson();

Update package.json

This feature allows you to update the contents of a package.json file. The code sample demonstrates how to change the version of the package and save the updated package.json file.

const { PackageJson } = require('@npmcli/package-json');

async function updatePackageJson() {
  const pkg = await PackageJson.load(process.cwd());
  pkg.update({ version: '1.0.1' });
  await pkg.save();
}

updatePackageJson();

Add dependency

This feature allows you to add a new dependency to the package.json file. The code sample demonstrates how to add a new package to the dependencies section and save the updated package.json file.

const { PackageJson } = require('@npmcli/package-json');

async function addDependency() {
  const pkg = await PackageJson.load(process.cwd());
  pkg.update({ dependencies: { 'new-package': '^1.0.0' } });
  await pkg.save();
}

addDependency();

Other packages similar to @npmcli/package-json

Keywords

npm

FAQs

Package last updated on 21 May 2025

Did you know?

Socket

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