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:
import { makePolicy, type RepopoConfig } from "repopo";import { PackageJsonSorted } from "repopo/policies";
export default { policies: [makePolicy(PackageJsonSorted)],} satisfies RepopoConfig;File headers
Section titled “File headers”HtmlFileHeaders
Section titled “HtmlFileHeaders”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.", },});| Option | Type | Description |
|---|---|---|
headerText | string | Required. The header text. |
autoGenText | string | Optional text appended to headerText. |
Configuration: Required. This policy has no default configuration.
Auto-fixable: Yes. --fix adds a missing header.
JsTsFileHeaders
Section titled “JsTsFileHeaders”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.
File extensions
Section titled “File extensions”NoJsFileExtensions
Section titled “NoJsFileExtensions”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.
Dependencies
Section titled “Dependencies”NoPrivateWorkspaceDependencies
Section titled “NoPrivateWorkspaceDependencies”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:
- Packages with
"private": trueare skipped, because they are never published. - The policy finds workspace packages from
pnpm-workspace.yamlor theworkspacesfield of the rootpackage.json. - A
workspace:dependency on a private package fails the check.
// Check dependencies only (default)makePolicy(NoPrivateWorkspaceDependencies);
// Or: also check devDependenciesmakePolicy(NoPrivateWorkspaceDependencies, { config: { checkDevDependencies: true } });| Option | Type | Default | Description |
|---|---|---|---|
checkDevDependencies | boolean | false | Also 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.
Package metadata
Section titled “Package metadata”PackageAllowedScopes
Section titled “PackageAllowedScopes”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"], },});| Option | Type | Description |
|---|---|---|
allowedScopes | string[] | Allowed scopes, such as @myorg. |
unscopedPackages | string[] | 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.
PackageEsmType
Section titled “PackageEsmType”Checks that the type field in package.json is set to "module" or "commonjs" as expected.
// Require ESM everywheremakePolicy(PackageEsmType, { config: { requiredType: "module" } });
// Or: infer the expected type from exports/mainmakePolicy(PackageEsmType, { config: { detectFromExports: true, onDetectionFailure: "warn" } });| Option | Type | Default | Description |
|---|---|---|---|
requiredType | "module" | "commonjs" | Type every package must declare. Overrides detection. | |
detectFromExports | boolean | false | Infer the expected type from exports and main. |
excludePackages | string[] | 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:
.mjsentry points orimportconditions mean"module"..cjsentry points orrequireconditions mean"commonjs".
Configuration: Required. This policy has no default configuration.
Auto-fixable: Yes. --fix sets the type field.
PackageFolderName
Section titled “PackageFolderName”Checks that the folder containing each package.json has the same name as the package.
makePolicy(PackageFolderName, { config: { stripScopes: ["@*"], excludePackages: ["@myorg/special-case"], },});| Option | Type | Description |
|---|---|---|
stripScopes | string[] | Scope globs to drop before comparing, such as @*, @myorg, or @my*. |
excludePackages | string[] | Package names to skip. |
| Package name | stripScopes | Expected 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.
PackageJsonProperties
Section titled “PackageJsonProperties”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.
PackageJsonRepoDirectoryProperty
Section titled “PackageJsonRepoDirectoryProperty”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.
PackageJsonSorted
Section titled “PackageJsonSorted”Checks that package.json files are sorted with sort-package-json.
Included in DefaultPolicies.
Auto-fixable: Yes.
PackageLicense
Section titled “PackageLicense”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 namemakePolicy(PackageLicense, { config: { skipPrivate: false, licenseFileName: "LICENSE.md" } });| Option | Type | Default | Description |
|---|---|---|---|
skipPrivate | boolean | true | Skip packages with "private": true. |
licenseFileName | string | "LICENSE" | License file name to compare and copy. |
Auto-fixable: Yes. --fix copies the root LICENSE into the package.
PackagePrivateField
Section titled “PackagePrivateField”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", },});| Option | Type | Default | Description |
|---|---|---|---|
mustPublish | string[] | Packages that must not have "private": true. | |
mustBePrivate | string[] | Packages that must have "private": true. | |
mayPublish | string[] | 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.
PackageReadme
Section titled “PackageReadme”Checks that each package has a README.md with the expected content.
makePolicy(PackageReadme, { config: { requireMatchingTitle: true, requiredContent: "## Trademark", },});| Option | Type | Default | Description |
|---|---|---|---|
skipPrivate | boolean | true | Skip packages with "private": true. |
requireMatchingTitle | boolean | true | Require the first line to be # <package name>. |
requiredContent | string | Text 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.
Scripts
Section titled “Scripts”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.
RequiredScripts
Section titled “RequiredScripts”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.
ExactScripts
Section titled “ExactScripts”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.
ConditionalScripts
Section titled “ConditionalScripts”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.
MutuallyExclusiveScripts
Section titled “MutuallyExclusiveScripts”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.
ScriptContains
Section titled “ScriptContains”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.
PackageScripts
Section titled “PackageScripts”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"]], },});| Option | Equivalent policy | Auto-fixable |
|---|---|---|
must | RequiredScripts | If a default is given |
exact | ExactScripts | Yes |
conditionalRequired | ConditionalScripts | If a default is given |
scriptMustContain | ScriptContains | No |
mutuallyExclusive | MutuallyExclusiveScripts | No |
PackageTestScripts
Section titled “PackageTestScripts”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"], },});| Option | Type | Default | Description |
|---|---|---|---|
testDirectories | string[] | Directories next to package.json that indicate tests. | |
testDependencies | string[] | devDependencies that indicate tests. | |
requiredScripts | string[] | ["test"] | Scripts required when tests are found. |
excludePackages | string[] | 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.