{"record":{"id":"d57d48a0baa307b1","repo":"yeasy/docker_practice","slug":"package-json-license-must-match-readme-license","errorCode":null,"errorMessage":"package.json license must match README license","messagePattern":"package\\.json license must match README license","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"scripts/check_metadata.js","lineNumber":14,"sourceCode":"const fs = require('fs');\n\nconst pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));\nconst changelog = fs.readFileSync('CHANGELOG.md', 'utf8');\nconst readme = fs.readFileSync('README.md', 'utf8');\n\nconst topVersion = changelog.match(/^\\* ([0-9]+\\.[0-9]+\\.[0-9]+)\\b/m)?.[1];\n\nif (pkg.version !== topVersion) {\n  throw new Error(`package.json version ${pkg.version} does not match CHANGELOG ${topVersion}`);\n}\n\nif (pkg.license !== 'CC-BY-NC-SA-4.0' || !readme.includes('CC BY-NC-SA 4.0')) {\n  throw new Error('package.json license must match README license');\n}\n","sourceCodeStart":1,"sourceCodeEnd":16,"githubUrl":"https://github.com/yeasy/docker_practice/blob/00d8a87f55c94ee632a5f506be01e4e38b5dedf1/scripts/check_metadata.js#L1-L16","documentation":"This error is the second gate in scripts/check_metadata.js, run via `npm test`. It enforces two license invariants at once: package.json's `license` field must be exactly the SPDX string 'CC-BY-NC-SA-4.0', and README.md must contain the human-readable string 'CC BY-NC-SA 4.0' (spaces, not hyphens). Because both conditions sit in one `if`, the error fires if either side drifts — even when only one file changed. It exists because this book (docker_practice) is published under a non-standard CC license and CI guards against accidental relicensing or README badge removal.","triggerScenarios":"Running `npm test` when (a) package.json \"license\" is anything other than the exact string CC-BY-NC-SA-4.0 (e.g. CC-BY-NC-SA-4.0/MIT, 'CC BY-NC-SA 4.0', or a standard SPDX id someone substituted), or (b) README.md no longer contains the literal substring 'CC BY-NC-SA 4.0' — the license badge line `![License: CC BY-NC-SA 4.0](...creativecommons.org...)` or the footer sentence `本书采用 [CC BY-NC-SA 4.0](...) 许可证。` was removed, reworded, or rewritten with hyphens (CC-BY-NC-SA-4.0) or a non-breaking space that does not byte-match.","commonSituations":"A dependency-scanning or license-normalization tool rewrites the license field to a different SPDX form; someone changes the license field to an OSI-approved id (MIT/Apache-2.0) to silence a registry warning; README restyling deletes or rewords the license badge/footer; localized README edits replace the space-separated license name with the hyphenated SPDX form, which fails the exact substring check.","solutions":["Restore package.json \"license\" to the exact string \"CC-BY-NC-SA-4.0\" (this is the project's declared license; do not change it to clear the error unless you own the relicensing decision).","Ensure README.md still contains the literal text 'CC BY-NC-SA 4.0' with plain spaces — typically by keeping the badge line `[![License: CC BY-NC-SA 4.0](https://img.shields.io/badge/...)](https://creativecommons.org/licenses/by-nc-sa/4.0/)` or the footer sentence linking to the license.","Rerun `node scripts/check_metadata.js` to confirm both gates pass (it checks version first, so fix error 0 too if present).","If the project truly relicenses, change the check string in scripts/check_metadata.js, package.json, and README together in one commit so the invariant stays coherent."],"exampleFix":"// package.json — before\n\"license\": \"MIT\"\n\n// package.json — after\n\"license\": \"CC-BY-NC-SA-4.0\"\n\n<!-- README.md – ensure this line exists -->\n[![License: CC BY-NC-SA 4.0](https://img.shields.io/badge/License-CC%20BY--NC--SA%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by-nc-sa/4.0/)","handlingStrategy":"validation","validationCode":"// Pre-flight the exact conditions check_metadata.js enforces\nconst fs = require('fs');\nconst pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));\nconst readme = fs.readFileSync('README.md', 'utf8');\nconst licenseOk = pkg.license === 'CC-BY-NC-SA-4.0';\nconst readmeOk = readme.includes('CC BY-NC-SA 4.0');\nif (!licenseOk) console.warn(`package.json license is ${pkg.license}; expected CC-BY-NC-SA-4.0`);\nif (!readmeOk) console.warn('README.md lost the literal string \"CC BY-NC-SA 4.0\" (check badge/footer)');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never let license-normalization tools or dependency bots rewrite the license field; pin it to CC-BY-NC-SA-4.0 (exact SPDX string, hyphenated).","When editing the README, keep at least one literal occurrence of 'CC BY-NC-SA 4.0' (spaces) — the badge line or the footer license sentence.","Treat any change to package.json license or the README license text as a release-review item: change both files and the check string together or not at all.","Run `node scripts/check_metadata.js` before opening a PR that touches README or package.json."],"tags":["ci","license","spdx","readme","compliance"],"backgroundTag":null,"analyzedSha":"00d8a87f55c94ee632a5f506be01e4e38b5dedf1","analyzedAt":"2026-08-15T23:18:50.239Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}