Streamlining Dependency Management and Updates with Claude Code
Learn about streamlining dependency management and updates using Claude Code. Practical tips and code examples included.
依存関係管理の課題
モダンなJavaScript/TypeScriptプロジェクトは数百のパッケージに依存しています。Claude Codeを活用すれば、依存関係の更新を安全かつ効率的に行えます。
依存関係の状態確認
Claude Codeに現在の依存関係を分析してもらいましょう。
プロジェクトの依存関係を分析して。
- 古くなっているパッケージのリスト
- セキュリティ脆弱性のあるパッケージ
- 使われていない依存関係
- 重複している依存関係
自動更新スクリプト
import { execSync } from "child_process";
import { readFileSync, writeFileSync } from "fs";
interface OutdatedPackage {
current: string;
wanted: string;
latest: string;
location: string;
}
function checkOutdated(): Record<string, OutdatedPackage> {
try {
const output = execSync("npm outdated --json", {
encoding: "utf-8",
});
return JSON.parse(output);
} catch (e: any) {
// npm outdated は古いパッケージがあると exit code 1
return JSON.parse(e.stdout || "{}");
}
}
function categorizeUpdates(
packages: Record<string, OutdatedPackage>
) {
const patch: string[] = [];
const minor: string[] = [];
const major: string[] = [];
for (const [name, info] of Object.entries(packages)) {
const [curMajor, curMinor] = info.current.split(".").map(Number);
const [latMajor, latMinor] = info.latest.split(".").map(Number);
if (latMajor > curMajor) {
major.push(name);
} else if (latMinor > curMinor) {
minor.push(name);
} else {
patch.push(name);
}
}
return { patch, minor, major };
}
安全な更新フロー
async function safeUpdate(packageName: string) {
console.log(`Updating ${packageName}...`);
// 1. 現在のlock fileをバックアップ
execSync("cp package-lock.json package-lock.json.bak");
try {
// 2. パッケージを更新
execSync(`npm install ${packageName}@latest`);
// 3. 型チェック
execSync("npx tsc --noEmit");
// 4. テスト実行
execSync("npm test");
// 5. ビルド確認
execSync("npm run build");
console.log(`${packageName} updated successfully`);
// バックアップをDelete
execSync("rm package-lock.json.bak");
} catch (error) {
console.error(`Update failed for ${packageName}, rolling back`);
execSync("cp package-lock.json.bak package-lock.json");
execSync("npm install");
throw error;
}
}
使われていない依存関係の検出
import depcheck from "depcheck";
async function findUnusedDeps(projectPath: string) {
const options = {
ignoreDirs: ["node_modules", "dist", "build"],
ignorePatterns: ["*.test.*", "*.spec.*"],
};
const result = await depcheck(projectPath, options);
console.log("未使用の dependencies:");
result.dependencies.forEach((dep) => console.log(` - ${dep}`));
console.log("\n未使用の devDependencies:");
result.devDependencies.forEach((dep) => console.log(` - ${dep}`));
console.log("\n不足している依存関係:");
for (const [dep, files] of Object.entries(result.missing)) {
console.log(` - ${dep} (used in: ${files.join(", ")})`);
}
return result;
}
Renovate/Dependabot設定の生成
Claude Codeに自動更新のCI設定を生成してもらいましょう。
// renovate.json
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["config:recommended"],
"schedule": ["every weekend"],
"packageRules": [
{
"matchUpdateTypes": ["patch"],
"automerge": true
},
{
"matchUpdateTypes": ["minor"],
"automerge": true,
"automergeType": "pr"
},
{
"matchUpdateTypes": ["major"],
"labels": ["breaking-change"],
"automerge": false
},
{
"matchPackageNames": ["typescript", "eslint"],
"groupName": "tooling"
}
]
}
セキュリティ監査の自動化
function securityAudit() {
try {
const output = execSync("npm audit --json", {
encoding: "utf-8",
});
const audit = JSON.parse(output);
const critical = audit.metadata.vulnerabilities.critical;
const high = audit.metadata.vulnerabilities.high;
if (critical > 0 || high > 0) {
console.error(
`Critical: ${critical}, High: ${high} vulnerabilities found`
);
// 自動修正を試みる
execSync("npm audit fix");
}
return audit;
} catch (e) {
console.error("Audit failed:", e);
throw e;
}
}
Claude Codeでの活用プロンプト
依存関係管理をClaude Codeに依頼するプロンプト例です。自動化の設定についてはフック機能ガイド、効率的な使い方は生産性を3倍にする10のTipsも参考にしてください。
依存関係を整理して。
- npm outdated で古いパッケージを確認
- patch と minor は一括更新してテスト実行
- major はひとつずつ更新して動作確認
- 使われていない依存関係があればDeleteして
- npm audit の脆弱性も修正して
依存関係管理のベストプラクティスはnpm公式ドキュメントを参照してください。Claude Codeの詳細は公式ドキュメントを確認しましょう。
Summary
依存関係の管理は地味ですが、セキュリティとメンテナンス性に直結する重要なタスクです。Claude Codeを活用すれば、更新の影響範囲を把握した上で安全にアップデートを進められます。
Related Posts
10 Tips to Triple Your Productivity with Claude Code
Learn about 10 tips to triple your productivity using Claude Code. Practical tips and code examples included.
Canvas/WebGL Optimization with Claude Code
Learn about canvas/webgl optimization using Claude Code. Practical tips and code examples included.
Markdown Implementation with Claude Code
Learn about markdown implementation using Claude Code. Practical tips and code examples included.