@kenai-platform/check-exact-packages
v1.0.2
Published
Enforce exact dependency versions (no ^ or ~) in package.json files
Downloads
15
Readme
@kenai-platform/check-exact-packages
A CLI tool to enforce exact dependency versions (no ^ or ~ prefixes) in all package.json files across your repository. This helps ensure reproducible builds and prevents unexpected dependency updates.
What it does
- Scans all
package.jsonfiles in the repository (including nested ones) - Checks all dependency types:
dependencies,devDependencies,peerDependencies, andoptionalDependencies - Detects non-exact versions that use
^(caret) or~(tilde) prefixes - Fails the check if any non-exact versions are found
- Provides detailed output showing which packages in which files have non-exact versions
Installation
Install the package from npm:
npm install --save-dev @kenai-platform/check-exact-packagesOr with bun:
bun add -d @kenai-platform/check-exact-packagesUsage
1. CLI Command
After installation, you can run the check from anywhere in your repository:
With npm/npx:
npx check-exact-packagesWith bun/bunx:
bunx @kenai-platform/check-exact-packagesIf installed globally:
npm install -g @kenai-platform/check-exact-packages
check-exact-packagesThe command will:
- Scan all
package.jsonfiles in your repository - Report any non-exact versions found
- Exit with code 1 if violations are found, 0 if all versions are exact
2. Preinstall Script (Run Without Installation)
You can run the check as a preinstall script without installing the package. This is useful for CI/CD pipelines or to enforce the check before dependencies are installed.
In your package.json:
{
"scripts": {
"preinstall": "npx @kenai-platform/check-exact-packages"
}
}Or with bun:
{
"scripts": {
"preinstall": "bunx @kenai-platform/check-exact-packages"
}
}Note: The preinstall script runs automatically before npm install or bun install. If non-exact versions are found, the installation will fail.
3. GitHub Actions Workflow
Add a GitHub Actions workflow to automatically check for exact versions on pull requests and pushes:
Create .github/workflows/check-exact-versions.yml:
name: Check Exact Versions
on:
pull_request:
paths:
- '**/package.json'
push:
branches: [main, master]
paths:
- '**/package.json'
jobs:
check-versions:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Run check-exact-packages
run: npx @kenai-platform/check-exact-packages4. Pre-commit Hook
Using pre-commit.com
Add this to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/Spookfish-ai/security-workflows
rev: v1.0.0 # Use the latest version tag
hooks:
- id: check-exact-packagesThen install and run:
pre-commit install
pre-commit run check-exact-packages --all-filesThe hook will automatically run before each commit.
Using Husky
If you're using Husky in your project:
Install the package:
npm install --save-dev @kenai-platform/check-exact-packagesAdd to your
.husky/pre-commitfile:#!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" npx check-exact-packages || exit 1Or with bun:
#!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" bunx @kenai-platform/check-exact-packages || exit 1
The hook will automatically run before each commit.
Prerequisites
- jq: Required to parse JSON files
- Pre-installed on GitHub Actions
ubuntu-latestrunners - For local use, install via:
brew install jq(macOS) orapt-get install jq(Linux)
- Pre-installed on GitHub Actions
- git: Required to find
package.jsonfiles (usesgit ls-files) - bash: Required to run the script
Example Output
When non-exact versions are found:
package.json has packages with non-exact versions:
• express: ^4.18.0
• lodash: ~4.17.21
Error: Use exact versions (no ^ or ~) in package.json filesWhen all versions are exact:
✓ All package.json files use exact versionsPublishing
This package is automatically published to npm when:
- Changes are pushed to
mainbranch that modifycheck-exact-packages.shorbin/check-exact-packages - The version in
package.jsonis manually updated
The publish workflow (publish.yml) will:
- Automatically bump the patch version if the script changes
- Publish to
@kenai-platform/check-exact-packageson npm - Create a git tag for the new version
Note: The workflow requires an NPM_TOKEN secret to be configured in GitHub Actions with publish permissions for the @kenai-platform scope.
Development
To contribute or modify this package:
- Clone the repository
- Make your changes
- Update the version in
package.jsonif needed - Test locally:
./check-exact-packages.shor./bin/check-exact-packages - Commit and push - the publish workflow will handle publishing
