Mohan pd.
Mohan pd. Author of The Coders Blog. Also a professional fullstack javascript developer working with various tech company and client around world.

Why should you use np over NPM to publish npm packages.

Sep 08, 2019 · 2 minutes
Why should you use np over NPM to publish npm packages.

If you work with npm registry and frequently use npm to publish your library then probably you should use industry standard alternative library call np. Previously, when I was maintaining public package of react component library, often I rely on npm which is not bad but after using np i think it’s a the better tool to do that. Here’s why:

  • Interactive UI
  • Ensures you are publishing from the master branch
  • Ensures the working directory is clean and that there are no unpulled changes
  • Reinstalls dependencies to ensure your project works with the latest dependency tree
  • Runs the tests
  • Bumps the version in package.json and npm-shrinkwrap.json (if present) and creates a git tag
  • Prevents accidental publishing of pre-release versions under the latest dist-tag
  • Publishes the new version to npm, optionally under a dist-tag
  • Rolls back the project to its previous state in case publishing fails
  • Pushes commits and tags (newly & previously created) to GitHub/GitLab
  • Supports two-factor authentication
  • Enables two-factor authentication on new repositories
  • (does not apply to external registries)
  • Opens a pre-filled GitHub Releases draft after publish
  • Warns about the possibility of extraneous files being published

Prerequisite & Installation

Node.js 8 or later
npm 6.8.0 or later
Git 2.11 or later

Install

npm install --global np
// or
yarn global add np

Usage

$ np --help

  Usage
    $ np <version>

    Version can be:
      patch | minor | major | prepatch | preminor | premajor | prerelease | 1.2.3

  Options
    --any-branch        Allow publishing from any branch
    --no-cleanup        Skips cleanup of node_modules
    --no-tests          Skips tests
    --yolo              Skips cleanup and testing
    --no-publish        Skips publishing
    --tag               Publish under a given dist-tag
    --no-yarn           Don't use Yarn
    --contents          Subdirectory to publish
    --no-release-draft  Skips opening a GitHub release draft

  Examples
    $ np
    $ np patch
    $ np 1.0.2
    $ np 1.0.2-beta.3 --tag=beta
    $ np 1.0.2-beta.3 --tag=beta --contents=dist

Config

{
	"name": "superb-package",
	"np": {
		"yarn": false,
		"contents": "dist"
	}
}

Prerequisite step runs forever on macOS

If you’re using macOS Sierra 10.12.2 or later, your SSH key passphrase is no longer stored into the keychain by default. This may cause the prerequisite step to run forever because it prompts for your passphrase in the background. To fix this, add the following lines to your ~/.ssh/config and run a simple Git command like git fetch.

Host *
 AddKeysToAgent yes
 UseKeychain yes

For more read to there github docs.