@achingbrain/dependency-check

中文文档
4.1.0发布于 2020/11/16BSD-3-ClauseCommonJS近 30 天下载 3498

checks which modules you have used in your code and then makes sure they are listed as dependencies in your package.json

原始使用文档
文档目录16 节
  1. requirements for maintained majors
  2. how it works
  3. cli usage
  4. --missing
  5. --unused
  6. --no-dev
  7. --no-peer
  8. --ignore-module, -i
  9. --no-default-entries
  10. --extensions, -e
  11. --detective
  12. --verbose
  13. --help
  14. auto check before every npm publish
  15. grunt usage
  16. protips

This is a temporary fork which is only necessary until dependency-check-team/dependency-check#127 or equivalent is merged

dependency-check

checks which modules you have used in your code and then makes sure they are listed as dependencies in your package.json, or vice-versa

Node CI Static code analysis dependencies Status Known Vulnerabilities

js-standard-style

requirements for maintained majors

dependency-check 4.x supports Node.js 10 and later

dependency-check 3.x supports Node.js 6 and later

dependency-check 2.x supports Node.js 0.10 and later (Dev note: published using the legacy tag)

For more info on maintenance status, see SECURITY.md.

how it works

dependency-check parses your module code starting from the default entry files (e.g. index.js or main and any bin commands defined in package.json or if specific files has been defined, then those) and traverses through all relatively required JS files, ultimately producing a list of non-relative modules

  • relative - e.g. require('./a-relative-file.js'), if one of these are encountered the required file will be recursively parsed by the dependency-check algorithm
  • non-relative - e.g. require('a-module'), if one of these are encountered it will get added to the list of dependencies, but sub-dependencies of the module will not get recursively parsed

the goal of this module is to simply check that all non-relative modules that get require()'d are in package.json, which prevents people from getting 'module not found' errors when they install your module that has missing deps which was accidentally published to NPM (happened to me all the time, hence the impetus to write this module).

cli usage

$ npm install dependency-check -g
$ dependency-check <path to module file(s), package.json or module folder>

# e.g.

$ dependency-check ./package.json --verbose
Success! All dependencies used in the code are listed in package.json
Success! All dependencies in package.json are used in the code
$ dependency-check ./package.json --missing --verbose
Success! All dependencies used in the code are listed in package.json
$ dependency-check ./package.json --unused --verbose
Success! All dependencies in package.json are used in the code

# or with file input instead:

$ dependency-check ./index.js

# even with globs and multiple inputs:

$ dependency-check ./test/**/*.js ./lib/*.js

dependency-check exits with code 1 if there are discrepancies, in addition to printing them out

To always exit with code 0 pass --ignore

--missing

running dependency-check ./package.json --missing will only do the check to make sure that all modules in your code are listed in your package.json

--unused

running dependency-check ./package.json --unused will only do the inverse of the missing check and will tell you which modules in your package.json dependencies were not used in your code

--no-dev

running dependency-check ./package.json --unused --no-dev will not tell you if any devDependencies in your package.json were missing or unused

--no-peer

running dependency-check ./package.json --unused --no-peer will not tell you if any peerDependencies in your package.json were missing or unused

--ignore-module, -i

ignores a module. This works for both --unused and --missing. You can specify as many separate --ignore-module arguments as you want. For example running dependency-check ./package.json --unused --ignore-module foo will not tell you if the foo module was not used in your code. Supports globbing patterns through the use of micromatch, so eg. --ignore-module "@types/*" is possible

--no-default-entries

running eg. dependency-check package.json tests.js --no-default-entries won't add any default entries despite the main path given being one to a package.json or module folder. So only the tests.js file will be checked

--extensions, -e

running dependency-check ./package.json -e js,jsx:precinct will resolve require paths to .js and .jsx paths, and parse using precinct.

--detective

running dependency-check ./package.json --detective precinct will require() the local precinct as the default parser. This can be set per-extension using using -e. Defaults to parsing with detective.

--verbose

Running with --verbose will enable a log message on success, otherwise dependency-check only logs on failure.

--help

shows above options and all other available options

auto check before every npm publish

add this to your .bash_profile/.bashrc

# originally from https://gist.github.com/mafintosh/405048d304fbabb830b2
npm () {
  ([ "$1" != "publish" ] || dependency-check .) && command npm "$@"
}

now when you do npm publish and you have missing dependencies it won't publish, e.g.:

$ npm publish
Fail! Dependencies not listed in package.json: siblings
$ npm install --save siblings
$ npm publish # works this time

grunt usage

See grunt-dependency-check.

protips

  • detective is used for parsing require() statements, which means it only does static requires. this means you should convert things like var foo = "bar"; require(foo) to be static, e.g. require("bar")
  • use globbing to effectively add all the files you want to check

版本与发布记录

@achingbrain/dependency-check 共发布 1 个版本,首次发布于 2020/11/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.1.0latest2020/11/165 年前

关于 @achingbrain/dependency-check 的常见问题

@achingbrain/dependency-check 是什么?

checks which modules you have used in your code and then makes sure they are listed as dependencies in your package.json

如何安装 @achingbrain/dependency-check?

在项目目录运行 npm install @achingbrain/dependency-check,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 dependency-check,可以用 npx dependency-check 直接运行。

@achingbrain/dependency-check 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/achingbrain__dependency-check 是否存在。

@achingbrain/dependency-check 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@achingbrain/dependency-check 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@achingbrain/dependency-check 可以商用吗?

@achingbrain/dependency-check 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@achingbrain/dependency-check 的下载量有多少?

根据 npm 官方下载统计,@achingbrain/dependency-check 在 2026/10/05 – 2026/11/03 这 30 天内被下载 3,498 次,日均约 117 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@achingbrain/dependency-check 还在维护吗?

最新版本 4.1.0 发布于 2020/11/16(5 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/11/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。