Skip to content

JavaScript and package.json policies

These policies check source file headers, file extensions, and the package.json files in a JavaScript or TypeScript repository. Policies that check package.json match every package.json file in the repository, including the root.

All examples go in the policies array of repopo.config.ts:

repopo.config.ts
import { makePolicy, type RepopoConfig } from "repopo";
import { PackageJsonSorted } from "repopo/policies";
export default {
policies: [makePolicy(PackageJsonSorted)],
} satisfies RepopoConfig;

Checks that .html and .htm files start with the configured header in an HTML comment (<!-- ... -->). Multi-line headers get comment delimiters on each line.

makePolicy(HtmlFileHeaders, {
config: {
headerText: "Copyright (c) Example Corp. All rights reserved.",
},
});
OptionTypeDescription
headerTextstringRequired. The header text.
autoGenTextstringOptional text appended to headerText.

Configuration: Required. This policy has no default configuration.

Auto-fixable: Yes. --fix adds a missing header.

Checks that JavaScript and TypeScript files (.js, .mjs, .cjs, .ts, .mts, .cts, .jsx, .tsx) start with the configured header in a /*! ... */ block comment. The ! keeps the comment during minification. Each line of a multi-line header gets a * prefix. A hashbang (#!) line stays first, and the header goes after it.

makePolicy(JsTsFileHeaders, {
config: {
headerText: "Copyright (c) Example Corp.\nLicensed under the MIT License.",
},
});

Takes the same options as HtmlFileHeaders.

Configuration: Required. This policy has no default configuration.

Auto-fixable: Yes.

Reports JavaScript files that use the .js extension. Node.js treats a .js file as CommonJS or ESM based on the type field of the nearest package.json, so changing that field changes how the file runs. The explicit .mjs and .cjs extensions remove that ambiguity.

Included in DefaultPolicies. Exclude generated or vendored files with the exclude option:

makePolicy(NoJsFileExtensions, { exclude: ["^bin/", "^vendor/"] });

Auto-fixable: No.

Stops publishable packages from depending on private workspace packages through the workspace: protocol. Publishing a package replaces each workspace: dependency with a real version. A private package ("private": true) is never published, so consumers can’t install the dependency.

How it works:

  1. Packages with "private": true are skipped, because they are never published.
  2. The policy finds workspace packages from pnpm-workspace.yaml or the workspaces field of the root package.json.
  3. A workspace: dependency on a private package fails the check.
// Check dependencies only (default)
makePolicy(NoPrivateWorkspaceDependencies);
// Or: also check devDependencies
makePolicy(NoPrivateWorkspaceDependencies, { config: { checkDevDependencies: true } });
OptionTypeDefaultDescription
checkDevDependenciesbooleanfalseAlso check devDependencies.

To fix a failure, publish the dependency (remove "private": true), move the shared code into a publishable package, or remove the dependency.

Auto-fixable: No.

Checks that every package uses an approved npm scope or is a listed unscoped name. This catches new or misspelled scopes in a monorepo.

makePolicy(PackageAllowedScopes, {
config: {
allowedScopes: ["@myorg", "@internal"],
unscopedPackages: ["legacy-tool"],
},
});
OptionTypeDescription
allowedScopesstring[]Allowed scopes, such as @myorg.
unscopedPackagesstring[]Unscoped package names that are allowed.

A scoped package must use a scope from allowedScopes. An unscoped package must appear in unscopedPackages. The policy always skips a package named root.

Configuration: Required. This policy has no default configuration.

Auto-fixable: No.

Checks that the type field in package.json is set to "module" or "commonjs" as expected.

// Require ESM everywhere
makePolicy(PackageEsmType, { config: { requiredType: "module" } });
// Or: infer the expected type from exports/main
makePolicy(PackageEsmType, { config: { detectFromExports: true, onDetectionFailure: "warn" } });
OptionTypeDefaultDescription
requiredType"module" | "commonjs"Type every package must declare. Overrides detection.
detectFromExportsbooleanfalseInfer the expected type from exports and main.
excludePackagesstring[]Package names or scopes to skip.
onDetectionFailure"skip" | "warn" | "fail""skip"What to do when detection finds no type.

Detection rules when detectFromExports is true:

  • .mjs entry points or import conditions mean "module".
  • .cjs entry points or require conditions mean "commonjs".

Configuration: Required. This policy has no default configuration.

Auto-fixable: Yes. --fix sets the type field.

Checks that the folder containing each package.json has the same name as the package.

makePolicy(PackageFolderName, {
config: {
stripScopes: ["@*"],
excludePackages: ["@myorg/special-case"],
},
});
OptionTypeDescription
stripScopesstring[]Scope globs to drop before comparing, such as @*, @myorg, or @my*.
excludePackagesstring[]Package names to skip.
Package namestripScopesExpected folder
@myorg/my-lib["@*"]my-lib
@myorg/my-lib["@myorg"]my-lib
@other/my-lib["@myorg"]@other/my-lib
my-lib["@*"]my-lib

Auto-fixable: No.

Checks that package.json fields match the configured values.

makePolicy(PackageJsonProperties, {
config: {
verbatim: {
license: "MIT",
author: "Example Author <author@example.com>",
repository: {
type: "git",
url: "git+https://github.com/example/repo.git",
},
},
},
});

With verbatim, every configured field must match its value exactly. A configured array replaces the existing array; it is not appended. Fields not in the configuration are left unchanged.

Configuration: Required. This policy has no default configuration.

Auto-fixable: Yes. --fix writes the configured values.

Checks that repository.directory in package.json is the package’s path relative to the repository root. Packages whose repository field is a string are skipped.

Included in DefaultPolicies.

Auto-fixable: Yes.

Checks that package.json files are sorted with sort-package-json.

Included in DefaultPolicies.

Auto-fixable: Yes.

Checks that each package has a LICENSE file identical to the one at the repository root. Each published package in a monorepo should include its own LICENSE.

// Skip private packages and look for LICENSE (defaults)
makePolicy(PackageLicense);
// Or: also require a license in private packages, with a different file name
makePolicy(PackageLicense, { config: { skipPrivate: false, licenseFileName: "LICENSE.md" } });
OptionTypeDefaultDescription
skipPrivatebooleantrueSkip packages with "private": true.
licenseFileNamestring"LICENSE"License file name to compare and copy.

Auto-fixable: Yes. --fix copies the root LICENSE into the package.

Checks the private field in package.json against glob patterns of package names. Use it to keep internal packages from being published, and to keep public packages from being blocked by "private": true.

makePolicy(PackagePrivateField, {
config: {
mustPublish: ["@myorg/*"],
mustBePrivate: ["@internal/*"],
mayPublish: ["@experimental/*"],
unmatchedPackages: "private",
},
});
OptionTypeDefaultDescription
mustPublishstring[]Packages that must not have "private": true.
mustBePrivatestring[]Packages that must have "private": true.
mayPublishstring[]Packages that can be either.
unmatchedPackages"private" | "public" | "ignore""private"Rule for packages that match no pattern.

Patterns are picomatch globs, checked in the order mustPublish, mustBePrivate, mayPublish. A package that matches none of them gets the unmatchedPackages rule.

Configuration: Required. This policy has no default configuration.

Auto-fixable: Yes. --fix adds or removes the private field.

Checks that each package has a README.md with the expected content.

makePolicy(PackageReadme, {
config: {
requireMatchingTitle: true,
requiredContent: "## Trademark",
},
});
OptionTypeDefaultDescription
skipPrivatebooleantrueSkip packages with "private": true.
requireMatchingTitlebooleantrueRequire the first line to be # <package name>.
requiredContentstringText that must appear in the README.

Auto-fixable: Yes. --fix creates a missing README with the package name as its title and appends missing requiredContent.

The script policies each check one rule. Combine them as needed. PackageScripts is an older policy that checks all of these rules together.

Several options take script entries. An entry is either a script name ("test"), which must exist but cannot be auto-fixed, or a one-key object ({ clean: "rimraf dist" }). With an object, --fix adds the script with that value when it is missing.

Checks that package.json contains the listed scripts. Only presence is checked, not content.

makePolicy(RequiredScripts, {
config: {
scripts: ["test", { lint: "biome lint ." }, { clean: "rimraf dist esm" }],
},
});

Configuration: Required. This policy has no default configuration.

Auto-fixable: Partial. --fix adds missing scripts that have a default value.

Checks that the listed scripts exist and have exactly the configured content.

makePolicy(ExactScripts, {
config: {
scripts: {
clean: "rimraf dist esm",
format: "biome format --write .",
},
},
});

Configuration: Required. This policy has no default configuration.

Auto-fixable: Yes. --fix adds missing scripts and replaces scripts with different content.

Requires other scripts when a given script is present.

makePolicy(ConditionalScripts, {
config: {
rules: [
// If "build" exists, "clean" must too
{ ifPresent: "build", requires: [{ clean: "rimraf dist" }] },
// If "test" exists, "test:coverage" must too
{ ifPresent: "test", requires: ["test:coverage"] },
],
},
});

Configuration: Required. This policy has no default configuration.

Auto-fixable: Partial. --fix adds missing scripts that have a default value.

Allows at most one script from each group. A package can have none of them.

makePolicy(MutuallyExclusiveScripts, {
config: {
groups: [
["lint:eslint", "lint:biome"],
["test:jest", "test:vitest"],
],
},
});

Configuration: Required. This policy has no default configuration.

Auto-fixable: No.

Checks that scripts contain the configured substrings. A script that doesn’t exist is not checked; use RequiredScripts to require it.

makePolicy(ScriptContains, {
config: {
rules: [
{ script: "clean", mustContain: ["rimraf"] },
{ script: "test", mustContain: ["vitest", "run"] },
],
},
});

Configuration: Required. This policy has no default configuration.

Auto-fixable: No.

Legacy policy that checks the rules of the five script policies above with one configuration. New configurations should use those policies instead.

makePolicy(PackageScripts, {
config: {
must: ["test", { build: "tsc" }],
exact: { clean: "rimraf dist esm" },
conditionalRequired: [{ ifPresent: "build", requires: [{ clean: "rimraf dist" }] }],
scriptMustContain: [{ script: "build", mustContain: ["tsc"] }],
mutuallyExclusive: [["lint:eslint", "lint:biome"]],
},
});
OptionEquivalent policyAuto-fixable
mustRequiredScriptsIf a default is given
exactExactScriptsYes
conditionalRequiredConditionalScriptsIf a default is given
scriptMustContainScriptContainsNo
mutuallyExclusiveMutuallyExclusiveScriptsNo

Requires test scripts in packages that contain test directories or test framework dependencies.

makePolicy(PackageTestScripts, {
config: {
testDirectories: ["test", "tests", "__tests__"],
testDependencies: ["vitest", "jest", "mocha"],
requiredScripts: ["test"],
excludePackages: ["@myorg/docs"],
},
});
OptionTypeDefaultDescription
testDirectoriesstring[]Directories next to package.json that indicate tests.
testDependenciesstring[]devDependencies that indicate tests.
requiredScriptsstring[]["test"]Scripts required when tests are found.
excludePackagesstring[]Package names or scopes to skip.

Only configured checks run. If both testDirectories and testDependencies are set, either one triggers the requirement.

Configuration: Required. This policy has no default configuration.

Auto-fixable: No.