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

publish-util

v2.1.0

Published

Some util to clean up package.json for npm publish

Downloads

236

Readme

publish-util

Clean up package.json for publishing.

Your package.json:

{
  "name": "...",
  "version": "...",
  "description": "...",
  "scripts": {
    "test": "...",
    "coverage": "...",
    "build": "...",
    "prepack": "npm run build && publish-util-prepack",
    "postpack": "publish-util-postpack"
  },
  "publishUtil": {
    "remove": ["devDependencies", { "scripts": ["test", "coverage", "build"] }],
    "keep": ["options"]
  },
  "dependencies": {
    "react": "^17.0.0",
    "react-dom": "^17.0.0"
  },
  "devDependencies": {
    "tap": "^15.0.0",
    "publish-util": "^1.0.0"
  },
  "nyc": {
    "reporter": ["lcov", "text", "text-summary"]
  },
  "options": {}
}

Your package.json published:

{
  "name": "...",
  "version": "...",
  "description": "...",
  "scripts": {
    "postpack": "publish-util-postpack"
  },
  "dependencies": {
    "react": "^17.0.0",
    "react-dom": "^17.0.0"
  },
  "options": {}
}

Usage

Just install this to your module:

npm install --save-dev publish-util

It will automatically add prepack and postpack scripts to your package.json for you.

Out of the box it will clean up non-standard fields plus workspaces and devDependencies from your package.json. To keep devDependencies, see details below

Publishing

This module offers a custom publishing script to fix some behaviors of npm publish.

The problem is that before any thing can process package.json, npm publish will load and upload it to the registry as packument (meta for the package).

If you want the copy of package.json after this module processed it to be the packument meta, then use the do-publish command:

npx do-publish

The script use whatever version of npm you have to do the actual work. It takes all CLI arguments that npm publish accepts and will pass them through.

publishUtil configs:

You can configure some behaviors with publishUtil in your package.json.

| Config | Description | Default | | ----------------- | ----------------------------------------------------------------------- | ------- | | rename | object map of keys to rename from package.json - done before remove | | | remove | array of keys and nesting keys to remove from package.json | | | keep | array of keys and nesting keys to keep from package.json. | | | removeExtraKeys | remove top level non-standard fields. | true | | autoPostPack | insert scripts.postpack if it's missing. | true | | silent | don't log message with console | false |

  • publishUtil is removed automatically.

  • rename details. The rename config is an object map, with key being the field to rename, and its value being the target name.

For example:

{
  "publishUtil": {
    "rename": {
      "scripts.foo": "scripts.renamedFoo"
    }
  }
}
  • scripts.prepublishOnly is removed automatically if it's just "publish-util-prepublishonly". Add it to publishUtil.keep to keep it:
{
  "publishUtil": {
    "keep": [{ "scripts": ["prepublishOnly"] }]
  }
}
  • Can't remove scripts.postpack because npm needs that to restore package.json.

  • Can't use prepack because npm publish uploads meta data before that so if you want to publish with different dependencies it will break.

remove and keep formats

The config remove and keep can be:

  • Array of strings that are either keys in the object or a regular expression to match keys in the object.
  • To specify a regular expression, use the format "/<regexp>/<flags>"
  • To reach into a nesting object, use an object of keys and array of keys/regex.

For example, to reach pkg.key1.key2, and pkg.key1.xyz*:

[
  {
    "key1": ["key2", "/xyz.*/"]
  }
]

Removing Non-standard Fields

These top level fields are considered standard fields:

  • all top level fields defined at npm package.json, except workspaces and devDependencies
  • And these fields: module

Any extra top level fields not in the standard fields are automatically removed.

  • To skip this automatic removal, set publishUtil.removeExtraKeys to false.
  • You can also add fields to publishUtil.keep to avoid them being removed.

For example, to keep devDependencies:

{
  "publishUtil": {
    "keep": ["devDependencies"]
  }
}

Auto postpack Insert

If you don't have a scripts.postpack, then it's automatically added with "publish-util-postpack" to ensure your package.json is restored after packing.

  • Set publishUtil.autoPostPack to false to skip this.

Dry Run and Verify

To verify package.json content.

  1. Run npm run prepack
  2. Inspect package.json to ensure everything is in order
  3. Run npm run postpack to restore package.json
  4. Inspect package.json again

Using npm pack

  1. Run npm pack
  2. Inspect package.json to ensure it's not modified
  3. Look for the .tgz file and extract it
  4. Inspect package/package.json to ensure it's as expected.