Skip to content

Instantly share code, notes, and snippets.

@zachshallbetter
Created January 13, 2025 11:21
Show Gist options
  • Save zachshallbetter/18af8fb5a189a033f0fcde29d1cb12bf to your computer and use it in GitHub Desktop.
Save zachshallbetter/18af8fb5a189a033f0fcde29d1cb12bf to your computer and use it in GitHub Desktop.
Update Peer Deps
// This script modifies the peerDependencies of conflicting packages directly within node_modules.
// Use Node.js to automate this process by adding "node resolve-peer-deps.js" to your package.json config
const fs = require('fs');
const path = require('path');
const packagesToUpdate = [
{
name: '@testing-library/react',
newPeerDependencies: {
react: '>=18.0.0 <20.0.0',
'react-dom': '>=18.0.0 <20.0.0',
},
},
{
name: 'another-library',
newPeerDependencies: {
react: '>=18.0.0 <20.0.0',
},
},
];
const resolvePath = (pkgName) => path.resolve('node_modules', pkgName, 'package.json');
const readJson = (filePath) => JSON.parse(fs.readFileSync(filePath, 'utf8'));
const writeJson = (filePath, data) => fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
const updatePeerDeps = ({ name, newPeerDependencies }) => {
try {
const packageJsonPath = resolvePath(name);
if (!fs.existsSync(packageJsonPath)) throw new Error(`Package not found: ${name}`);
const packageJson = readJson(packageJsonPath);
packageJson.peerDependencies = { ...packageJson.peerDependencies, ...newPeerDependencies };
writeJson(packageJsonPath, packageJson);
console.log(`[Updated] ${name} peerDependencies`);
} catch (error) {
console.error(`[Error] ${name}: ${error.message}`);
}
};
packagesToUpdate.forEach(updatePeerDeps);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment