fix(install): add package.json to prevent ESM inheritance (#602)

Projects with "type": "module" in package.json cause GSD scripts to fail
with "require is not defined" because Node.js walks up the directory tree
and inherits the module type.

Fix: Write {"type":"commonjs"} package.json to the install target (.claude/)
during installation. This stops Node from inheriting the project's ESM config.

- Install: writes package.json after VERSION file
- Uninstall: removes package.json only if it matches our marker

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Lex Christopherson
2026-02-15 07:44:26 -06:00
parent 9d815d31db
commit 5154446056

View File

@@ -867,7 +867,23 @@ function uninstall(isGlobal, runtime = 'claude') {
}
}
// 5. Clean up settings.json (remove GSD hooks and statusline)
// 5. Remove GSD package.json (CommonJS mode marker)
const pkgJsonPath = path.join(targetDir, 'package.json');
if (fs.existsSync(pkgJsonPath)) {
try {
const content = fs.readFileSync(pkgJsonPath, 'utf8').trim();
// Only remove if it's our minimal CommonJS marker
if (content === '{"type":"commonjs"}') {
fs.unlinkSync(pkgJsonPath);
removedCount++;
console.log(` ${green}✓${reset} Removed GSD package.json`);
}
} catch (e) {
// Ignore read errors
}
}
// 6. Clean up settings.json (remove GSD hooks and statusline)
const settingsPath = path.join(targetDir, 'settings.json');
if (fs.existsSync(settingsPath)) {
let settings = readSettings(settingsPath);
@@ -1405,6 +1421,13 @@ function install(isGlobal, runtime = 'claude') {
failures.push('VERSION');
}
// Write package.json to force CommonJS mode for GSD scripts
// Prevents "require is not defined" errors when project has "type": "module"
// Node.js walks up looking for package.json - this stops inheritance from project
const pkgJsonDest = path.join(targetDir, 'package.json');
fs.writeFileSync(pkgJsonDest, '{"type":"commonjs"}\n');
console.log(` ${green}✓${reset} Wrote package.json (CommonJS mode)`);
// Copy hooks from dist/ (bundled with dependencies)
const hooksSrc = path.join(src, 'hooks', 'dist');
if (fs.existsSync(hooksSrc)) {