Skip to content

Instantly share code, notes, and snippets.

@Dkendal
Last active December 5, 2018 17:05
Show Gist options
  • Save Dkendal/06af1eeda2a0216ad29ca656f3a9794c to your computer and use it in GitHub Desktop.
Save Dkendal/06af1eeda2a0216ad29ca656f3a9794c to your computer and use it in GitHub Desktop.
Node js script to set exact dependencies based on what is installed in node_modules.
'use strict';
const fs = require('fs');
const pwd = process.cwd();
const pkg = require(`${pwd}/package.json`);
for (const key of [
'devDependencies',
'dependencies',
'peerDependencies',
'optionalDependencies',
]) {
const entry = pkg[key];
if (!entry) {
continue;
}
for (const [dep, _version] of Object.entries(entry)) {
const localpkg = fs.readFileSync(
`${pwd}/node_modules/${dep}/package.json`,
{
encoding: 'utf8',
}
);
const version = JSON.parse(localpkg).version;
pkg[key][dep] = version;
}
}
console.log(JSON.stringify(pkg, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment