exact-deps

1.2.1 • Public • Published

exact-deps

Update package.json to use exact versions for dependencies

NOTE: this does not replace the best practice of adding a .npmrc file with save-exact=true attribute to a project

yarn add -D exact-deps
Table of Contents

Getting started

For a variety of reasons, I have often wanted to convert all the versions of dependencies in a package.json file to the exact versions that have been installed.

exact-deps solves this problem by

  • looping through each dependency in the main package.json file
  • for each dependency:
    • finding the locally installed dependency's package.json file
    • updating the entry in the main package.json with the version attribute from the dependency's package.json file

Requirements

  • node: >=8

Command Line

This module provides a simple CLI:

./node_modules/.bin/exact-deps

If combined with Yarn, it can be run as:

yarn exact-deps package.json

It can also be used as part of an npm script:

{
  "scripts": {
    "deps:exact": "exact-deps"
  },
  "devDependencies": {
    "exact-deps": "latest"
  }
}
yarn deps:exact

Options

Option Alias Description Default
prefix p Prefix to put before each version ''
help h Print help menu

Module

The module exports a function that takes the directory of package.json and a prefix.

It returns a new object with path and contents properties

const fs = require('fs');
const exactDeps = require('exact-deps');
 
const { path, contents } = exactDeps(process.cwd(), '^');
fs.writeFileSync(path, JSON.stringify(contents, null, 2));

Integrating

An effective integration of this plugin could look like this:

{
  "scripts": {
    "deps:exact": "exact-deps",
    "precommit": "lint-staged",
    "prepublish": "deps:exact"
  },
  "lint-staged": {
    "package.json": [
      "exact-deps -p ^",
      "git add"
    ]
  },
  "devDependencies": {
    "lint-staged": "latest",
    "exact-deps": "latest"
  },
  "optionalDependencies": {
    "husky": "latest"
  }
}

This configuration combines:

  • lint-staged for automatically running tasks on staged files
  • husky for githook integrations
  • exact-deps to make sure package.json is always exact

Together, these modules ensure the package.json file is automatically updated if it changes and provides an easy package.json script for manual use:

yarn deps:exact

Package Sidebar

Install

npm i exact-deps

Weekly Downloads

1

Version

1.2.1

License

MIT

Last publish

Collaborators

  • camacho