npm发布前检查工具

用于在npm包发布前自动执行构建、校验入口文件、打包安装并验证模块可加载性的开发辅助工具,适用于npm包发布流程的质量保障。

  • by kmalakoff
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 kmalakoff/npm-check-prepublish

使用说明

npm-check-prepublish

Check the package npm would publish: build it, verify declared entry files, pack and install it in a temporary project, then load its module entry points or run its CLI with --version.

Install

npm install --save-dev npm-check-prepublish

Add it to the package being checked:

{
  "scripts": {
    "prepublishOnly": "npm-check-prepublish"
  }
}

Run it directly with npx npm-check-prepublish or the shorter installed binary, ncp.

The check runs npm run build when that script exists. It then:

  • verifies files referenced by main, module, types, and bin, plus any configured requiredFiles;
  • creates a tarball and installs it with production dependencies in a temporary directory;
  • rejects packaged top-level src and test entries, and any top-level name starting with .env (such as .env.test or .envrc);
  • for a package without bin, loads the exports["."] require entry (falling back to main), and the import entry when it names a different file and the runtime supports ESM; or
  • for a package with bin, runs the first declared binary with --version.

It checks package structure and entry-point loading. It does not run the package's test suite or prove every runtime path works.

Options

Skip individual checks when they do not fit a package:

ncp --no-build
ncp --no-check-required-files
ncp --no-pack
ncp --no-check-import
ncp --no-check-bin

Configuration can live in .ncprc.json or the ncp field of package.json:

{
  "ncp": {
    "requiredFiles": ["LICENSE"],
    "skipCheckBin": true
  }
}

CLI flags override file configuration. requiredFiles are added to the paths inferred from package.json.

Programmatic API

import { CheckPrepublish } from 'npm-check-prepublish';

const result = await new CheckPrepublish({
  packageDir: process.cwd(),
  requiredFiles: ['LICENSE']
}).check();

if (!result.success) console.error(result.errors);

check() returns { success, errors, packageInfo? }, where errors is an array of strings. Programmatic options use skipBuild, skipCheckRequiredFiles, skipPackage, skipCheckImport, and skipCheckBin.

License

MIT