Skip to content

Instantly share code, notes, and snippets.

@manuartero
Created June 30, 2025 14:22
Show Gist options
  • Save manuartero/8286c392d7360b121688bcd4389c4d87 to your computer and use it in GitHub Desktop.
Save manuartero/8286c392d7360b121688bcd4389c4d87 to your computer and use it in GitHub Desktop.
replace caret with exact version on the package.json
#!/usr/bin/env node
/* node exact-versions.cjs */
'use strict';
const fs = require('fs');
const pkg = JSON.parse(fs.readFileSync('../package.json'));
const lock = JSON.parse(fs.readFileSync('../package-lock.json'));
['dependencies', 'devDependencies'].forEach(section => {
if (!pkg[section]) return;
Object.keys(pkg[section]).forEach(dep => {
console.log('------------');
console.log(`Checking ${section}: ${dep}`);
const lockVersion = lock.packages?.[`node_modules/${dep}`]?.version;
if (!lockVersion) {
console.warn(`Warning: ${dep} not found in lock file`);
return;
}
console.log(`Lock file has ${dep}: ${lockVersion}`);
if (pkg[section][dep] !== lockVersion) {
console.log(`Updating ${section}: ${dep} to exact version from lock file`);
pkg[section][dep] = lockVersion;
} else {
console.log(`No update needed for ${section}: ${dep}`);
}
});
});
fs.writeFileSync('../package.json', JSON.stringify(pkg, null, 2));
console.log('Updated package.json with exact versions; now run npm install to update package-lock.json');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment