Created
January 13, 2025 11:21
-
-
Save zachshallbetter/18af8fb5a189a033f0fcde29d1cb12bf to your computer and use it in GitHub Desktop.
Update Peer Deps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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