npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@financial-times/package-json

v4.0.0

Published

This library allows you to load, manipulate and write the contents of a [`package.json`](https://docs.npmjs.com/files/package.json.html) file. It also provides a [changelog](#getchangelog) detailing any changes that have been made.

Downloads

1,865

Readme

package-json

This library allows you to load, manipulate and write the contents of a package.json file. It also provides a changelog detailing any changes that have been made.

Usage

npm install @financial-times/package-json

loadPackageJson

After loading the specified package.json file into memory, the loadPackageJson method returns a collection of methods that can be used for changing the package.json document and writing those changes back to disk.

const loadPackageJson = require("@financial-times/package-json");
const packageJson = loadPackageJson({ filepath: `filepath/to/package.json` });

Methods returned:

get

Returns an object representing the current working state of the package.json document. This may be different to what exists on the file system if changes have not yet been written by calling the writeChanges method.

packageJson.get();

hasChangesToWrite

Checks if there are file changes to write.

packageJson.hasChangesToWrite(); // true or false

writeChanges

Writes to the package.json file.

packageJson.writeChanges(); // true

getField

Gets a specific field from the package.json object, by passing the field as an argument.

packageJson.getField("name"); // "@financial-times/package-json"

setField

Sets the value for a specific field in the package.json object and returns a changelog entry.

packageJson.setField("name", "newName");

Returns a changelog entry object:

{
  "event": "setField",
  "field": "name",
  "meta": {},
  "previousValue": "oldName",
  "alreadyExisted": false
}

removeField

Removes a specific field in the package.json object and returns a changelog entry.

packageJson.removeField("license");

Returns a changelog entry object:

{
  "event": "removeField",
  "field": "license",
  "meta": {},
  "previousValue": "MIT",
  "alreadyExisted": true
}

requireDependency

Requires a package to exist as a dependency in package.json.

packageJson.requireDependency({
  pkg: "prettier",
  version: "1.16.4",
  field: "devDependencies"
});

Returns a changelog entry object:

{
  "event": "requireDependency",
  "field": "devDependencies",
  "meta": {
    "pkg": "prettier",
    "version": "1.16.4"
  },
  "previousValue": "1.16.3",
  "alreadyExisted": true
}

removeDependency

Removes a package as a dependency from package.json.

packageJson.removeDependency({
  pkg: "prettier",
  version: "1.16.4",
  field: "devDependencies"
});

Returns a changelog entry object, or false if the dependency doesn't exist:

{
  "event": "removeDependency",
  "field": "devDependencies",
  "meta": {
    "pkg": "prettier"
  },
  "previousValue": "1.16.3",
  "alreadyExisted": true
}

requireScript

Requires a script to exist in the scripts field of package.json.

packageJson.requireScript({
  stage: "test",
  command: "npm run unit-test"
});

Returns a changelog entry object:

{
  "event": "requireScript",
  "field": "scripts",
  "meta": {
    "stage": "test"
  },
  "alreadyExisted": true
}

removeScript

Requires a script to exist in the scripts field of package.json.

packageJson.removeScript({
  stage: "lint"
});

Returns a changelog entry object:

{
  "event": "removeScript",
  "field": "scripts",
  "meta": {
    "stage": "lint"
  },
  "alreadyExisted": true
}

getChangelog

The changelog represents all the changes that have been made to the package.json object, regardless of whether they have yet been written to the file.

The changelog is made up of entry objects, which all have the following properties:

  • event - The type of event i.e. setField, requireDependency, removeDependency or requireScript
  • field - The field in package.json that was changed
  • alreadyExisted - Flag whether the field already existed
  • previousValue - Previous value of the field
  • meta - An object containing extra details about the change e.g. pkg, version, stage

You can access the changelog entries with the following methods:

  • getChangelog()
  • getChangelog.asMessages()
  • getChangelog.lastEntry()
  • getChangelog.lastEntryAsMessage()

Examples of working with the changelog

packageJson.requireDependency({
  pkg: "prettier",
  version: "1.16.4",
  field: "devDependencies"
});

packageJson.requireScript({
  stage: "test",
  command: "npm run unit-test"
});

const changelogObjects = packageJson.getChangelog();

/*
[
  {
    event: "requireDependency",
    field: "devDependencies",
    meta: {
      pkg: "prettier",
      version: "1.16.4"
    },
    previousValue: "1.16.3",
    alreadyExisted: true
  },
  {
    event: "requireScript",
    field: "scripts",
    meta: {
      stage: "test"
    },
    alreadyExisted: true
  }
]
*/

const changelogMessages = packageJson.getChangelog.asMessages();

/*
[
  "Required package [email protected] in devDependencies, previously 1.16.3",
  "Required script for stage 'test' (overwrote existing command)"
]
*/

const lastChangelogEntryObject = packageJson.getChangelog.lastEntry();

/*
{
  event: "requireScript",
  field: "scripts",
  meta: {
    stage: "test"
  },
  alreadyExisted: true
}
*/

const lastChangelogEntryMessage = packageJson.getChangelog.lastEntryAsMessage();

// "Required script for stage 'test' (overwrote existing command)"