yeasy/docker_practice · error · Error

package.json license must match README license

Error message

package.json license must match README license

What it means

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.

Source

Thrown at scripts/check_metadata.js:14

const fs = require('fs');

const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const changelog = fs.readFileSync('CHANGELOG.md', 'utf8');
const readme = fs.readFileSync('README.md', 'utf8');

const topVersion = changelog.match(/^\* ([0-9]+\.[0-9]+\.[0-9]+)\b/m)?.[1];

if (pkg.version !== topVersion) {
  throw new Error(`package.json version ${pkg.version} does not match CHANGELOG ${topVersion}`);
}

if (pkg.license !== 'CC-BY-NC-SA-4.0' || !readme.includes('CC BY-NC-SA 4.0')) {
  throw new Error('package.json license must match README license');
}

View on GitHub (pinned to 00d8a87f55)

Solutions

  1. 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).
  2. 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.
  3. Rerun `node scripts/check_metadata.js` to confirm both gates pass (it checks version first, so fix error 0 too if present).
  4. 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.

Example fix

// package.json — before
"license": "MIT"

// package.json — after
"license": "CC-BY-NC-SA-4.0"

<!-- README.md – ensure this line exists -->
[![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/)
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight the exact conditions check_metadata.js enforces
const fs = require('fs');
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
const readme = fs.readFileSync('README.md', 'utf8');
const licenseOk = pkg.license === 'CC-BY-NC-SA-4.0';
const readmeOk = readme.includes('CC BY-NC-SA 4.0');
if (!licenseOk) console.warn(`package.json license is ${pkg.license}; expected CC-BY-NC-SA-4.0`);
if (!readmeOk) console.warn('README.md lost the literal string "CC BY-NC-SA 4.0" (check badge/footer)');

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of yeasy/docker_practice@00d8a87f55 (2026-08-15). Data as JSON: /api/errors/d57d48a0baa307b1. Report an issue: GitHub.