另一种解决方案
function dependenciesNeedUpdating() {
const childProcess = require('child_process');
const result = JSON.parse(childProcess.execSync('npm install --dry-run --json').toString());
return result.added.length > 0 || result.updated.length > 0 || result.removed > 0;
}
这样称呼
if (dependenciesNeedUpdating()) {
console.error('dependencies need updating. Please run `npm install`');
process.exit(1);
}
如果你想将它作为依赖安装? 它的 5 行在一个 npm 包here 中。它添加了一个 ld-check-dependencies 命令(上面的 4 行使用),因此您可以直接在 npm 脚本中使用它。示例:
"scripts": {
"build": "ld-check-dependencies && rollup ..."
},
理由:
我了解到依赖是一个巨大的负担,只要有更简单的解决方案就应该避免。
例如,这里是 check-dependencies 的可笑依赖树
└─┬ check-dependencies@1.1.0
├─┬ bower-config@1.4.3
│ ├── graceful-fs@4.2.4
│ ├── minimist@0.2.1 extraneous
│ ├── mout@1.2.2
│ ├─┬ osenv@0.1.5
│ │ ├── os-homedir@1.0.2
│ │ └── os-tmpdir@1.0.2
│ ├─┬ untildify@2.1.0
│ │ └── os-homedir@1.0.2 deduped
│ └── wordwrap@0.0.3
├─┬ chalk@2.4.2
│ ├─┬ ansi-styles@3.2.1
│ │ └─┬ color-convert@1.9.3
│ │ └── color-name@1.1.3
│ ├── escape-string-regexp@1.0.5
│ └─┬ supports-color@5.5.0
│ └── has-flag@3.0.0
├─┬ findup-sync@2.0.0
│ ├── detect-file@1.0.0
│ ├─┬ is-glob@3.1.0
│ │ └── is-extglob@2.1.1
│ ├─┬ micromatch@3.1.10
│ │ ├── arr-diff@4.0.0
│ │ ├── array-unique@0.3.2
│ │ ├─┬ braces@2.3.2
│ │ │ ├── arr-flatten@1.1.0
│ │ │ ├── array-unique@0.3.2 deduped
│ │ │ ├── extend-shallow@2.0.1 extraneous
│ │ │ ├─┬ fill-range@4.0.0
│ │ │ │ ├── extend-shallow@2.0.1 extraneous
│ │ │ │ ├─┬ is-number@3.0.0
│ │ │ │ │ └── kind-of@3.2.2 extraneous
│ │ │ │ ├── repeat-string@1.6.1
│ │ │ │ └─┬ to-regex-range@2.1.1
│ │ │ │ ├── is-number@3.0.0 deduped
│ │ │ │ └── repeat-string@1.6.1 deduped
│ │ │ ├── isobject@3.0.1
│ │ │ ├── repeat-element@1.1.3
│ │ │ ├── snapdragon@0.8.2 deduped
│ │ │ ├─┬ snapdragon-node@2.1.1
│ │ │ │ ├── define-property@1.0.0 extraneous
│ │ │ │ ├── isobject@3.0.1 deduped
│ │ │ │ └─┬ snapdragon-util@3.0.1
│ │ │ │ └── kind-of@3.2.2 extraneous
│ │ │ ├─┬ split-string@3.1.0
│ │ │ │ └── extend-shallow@3.0.2 deduped
│ │ │ └── to-regex@3.0.2 deduped
│ │ ├─┬ define-property@2.0.2
│ │ │ ├── is-descriptor@1.0.2 extraneous
│ │ │ └── isobject@3.0.1 deduped
│ │ ├─┬ extend-shallow@3.0.2
│ │ │ ├── assign-symbols@1.0.0
│ │ │ └── is-extendable@1.0.1 extraneous
│ │ ├─┬ extglob@2.0.4
│ │ │ ├── array-unique@0.3.2 deduped
│ │ │ ├── define-property@1.0.0 extraneous
│ │ │ ├─┬ expand-brackets@2.1.4
│ │ │ │ ├── debug@2.6.9 deduped
│ │ │ │ ├── define-property@0.2.5 extraneous
│ │ │ │ ├── extend-shallow@2.0.1 extraneous
│ │ │ │ ├── posix-character-classes@0.1.1
│ │ │ │ ├── regex-not@1.0.2 deduped
│ │ │ │ ├── snapdragon@0.8.2 deduped
│ │ │ │ └── to-regex@3.0.2 deduped
│ │ │ ├── extend-shallow@2.0.1 extraneous
│ │ │ ├── fragment-cache@0.2.1 deduped
│ │ │ ├── regex-not@1.0.2 deduped
│ │ │ ├── snapdragon@0.8.2 deduped
│ │ │ └── to-regex@3.0.2 deduped
│ │ ├─┬ fragment-cache@0.2.1
│ │ │ └── map-cache@0.2.2
│ │ ├── kind-of@6.0.3
│ │ ├─┬ nanomatch@1.2.13
│ │ │ ├── arr-diff@4.0.0 deduped
│ │ │ ├── array-unique@0.3.2 deduped
│ │ │ ├── define-property@2.0.2 deduped
│ │ │ ├── extend-shallow@3.0.2 deduped
│ │ │ ├── fragment-cache@0.2.1 deduped
│ │ │ ├── is-windows@1.0.2
│ │ │ ├── kind-of@6.0.3 deduped
│ │ │ ├── object.pick@1.3.0 deduped
│ │ │ ├── regex-not@1.0.2 deduped
│ │ │ ├── snapdragon@0.8.2 deduped
│ │ │ └── to-regex@3.0.2 deduped
│ │ ├─┬ object.pick@1.3.0
│ │ │ └── isobject@3.0.1 deduped
│ │ ├─┬ regex-not@1.0.2
│ │ │ ├── extend-shallow@3.0.2 deduped
│ │ │ └─┬ safe-regex@1.1.0
│ │ │ └── ret@0.1.15
│ │ ├─┬ snapdragon@0.8.2
│ │ │ ├─┬ base@0.11.2
│ │ │ │ ├─┬ cache-base@1.0.1
│ │ │ │ │ ├─┬ collection-visit@1.0.0
│ │ │ │ │ │ ├─┬ map-visit@1.0.0
│ │ │ │ │ │ │ └── object-visit@1.0.1 deduped
│ │ │ │ │ │ └─┬ object-visit@1.0.1
│ │ │ │ │ │ └── isobject@3.0.1 deduped
│ │ │ │ │ ├── component-emitter@1.3.0 deduped
│ │ │ │ │ ├── get-value@2.0.6
│ │ │ │ │ ├─┬ has-value@1.0.0
│ │ │ │ │ │ ├── get-value@2.0.6 deduped
│ │ │ │ │ │ ├─┬ has-values@1.0.0
│ │ │ │ │ │ │ ├── is-number@3.0.0 deduped
│ │ │ │ │ │ │ └── kind-of@4.0.0 extraneous
│ │ │ │ │ │ └── isobject@3.0.1 deduped
│ │ │ │ │ ├── isobject@3.0.1 deduped
│ │ │ │ │ ├─┬ set-value@2.0.1
│ │ │ │ │ │ ├── extend-shallow@2.0.1 extraneous
│ │ │ │ │ │ ├── is-extendable@0.1.1
│ │ │ │ │ │ ├─┬ is-plain-object@2.0.4
│ │ │ │ │ │ │ └── isobject@3.0.1 deduped
│ │ │ │ │ │ └── split-string@3.1.0 deduped
│ │ │ │ │ ├─┬ to-object-path@0.3.0
│ │ │ │ │ │ └── kind-of@3.2.2 extraneous
│ │ │ │ │ ├─┬ union-value@1.0.1
│ │ │ │ │ │ ├── arr-union@3.1.0 deduped
│ │ │ │ │ │ ├── get-value@2.0.6 deduped
│ │ │ │ │ │ ├── is-extendable@0.1.1 deduped
│ │ │ │ │ │ └── set-value@2.0.1 deduped
│ │ │ │ │ └─┬ unset-value@1.0.0
│ │ │ │ │ ├── has-value@0.3.1 extraneous
│ │ │ │ │ └── isobject@3.0.1 deduped
│ │ │ │ ├─┬ class-utils@0.3.6
│ │ │ │ │ ├── arr-union@3.1.0
│ │ │ │ │ ├── define-property@0.2.5 extraneous
│ │ │ │ │ ├── isobject@3.0.1 deduped
│ │ │ │ │ └─┬ static-extend@0.1.2
│ │ │ │ │ ├── define-property@0.2.5 extraneous
│ │ │ │ │ └─┬ object-copy@0.1.0
│ │ │ │ │ ├── copy-descriptor@0.1.1
│ │ │ │ │ ├── define-property@0.2.5 extraneous
│ │ │ │ │ └── kind-of@3.2.2 extraneous
│ │ │ │ ├── component-emitter@1.3.0
│ │ │ │ ├── define-property@1.0.0 extraneous
│ │ │ │ ├── isobject@3.0.1 deduped
│ │ │ │ ├─┬ mixin-deep@1.3.2
│ │ │ │ │ ├── for-in@1.0.2
│ │ │ │ │ └── is-extendable@1.0.1 extraneous
│ │ │ │ └── pascalcase@0.1.1
│ │ │ ├─┬ debug@2.6.9
│ │ │ │ └── ms@2.0.0
│ │ │ ├── define-property@0.2.5 extraneous
│ │ │ ├── extend-shallow@2.0.1 extraneous
│ │ │ ├── map-cache@0.2.2 deduped
│ │ │ ├── source-map@0.5.7
│ │ │ ├─┬ source-map-resolve@0.5.3
│ │ │ │ ├── atob@2.1.2
│ │ │ │ ├── decode-uri-component@0.2.0
│ │ │ │ ├── resolve-url@0.2.1
│ │ │ │ ├── source-map-url@0.4.0
│ │ │ │ └── urix@0.1.0
│ │ │ └── use@3.1.1
│ │ └─┬ to-regex@3.0.2
│ │ ├── define-property@2.0.2 deduped
│ │ ├── extend-shallow@3.0.2 deduped
│ │ ├── regex-not@1.0.2 deduped
│ │ └── safe-regex@1.1.0 deduped
│ └─┬ resolve-dir@1.0.1
│ ├─┬ expand-tilde@2.0.2
│ │ └─┬ homedir-polyfill@1.0.3
│ │ └── parse-passwd@1.0.0
│ └─┬ global-modules@1.0.0
│ ├─┬ global-prefix@1.0.2
│ │ ├── expand-tilde@2.0.2 deduped
│ │ ├── homedir-polyfill@1.0.3 deduped
│ │ ├── ini@1.3.5
│ │ ├── is-windows@1.0.2 deduped
│ │ └─┬ which@1.3.1
│ │ └── isexe@2.0.0
│ ├── is-windows@1.0.2 deduped
│ └── resolve-dir@1.0.1 deduped
├── lodash.camelcase@4.3.0
├── minimist@1.2.5
└── semver@5.7.1
788个js文件,48000行代码。
这些依赖项中的每一个都是另一个让您被告知某些东西已损坏、已弃用、存在新漏洞等的机会。基本上,通过使用具有如此多依赖项的东西,您会增加您的工作量,而您本可以花费运送时间,跳舞,和孩子一起玩,等等。您认为通过使用依赖项可以节省时间,但是如果它有这么多的依赖项,那么如果与较低或 simple 无依赖项选项相比,您并没有节省时间,因为您将永远处理这些依赖关系的问题。
这是deps-ok 的依赖关系树,它更合理。
└─┬ deps-ok@1.4.1
├── check-more-types@2.24.0
├─┬ debug@3.1.0
│ └── ms@2.0.0
├── lazy-ass@1.6.0
├── lodash@4.17.10
├── minimist@1.2.0
├─┬ q@2.0.3
│ ├── asap@2.0.6
│ ├── pop-iterate@1.0.1
│ └── weak-map@1.0.5
├── quote@0.4.0
└── semver@5.5.0
但它仍然设法证明了依赖关系的问题
❯ npm audit
=== npm audit security report ===
┌──────────────────────────────────────────────────────────────────────────────┐
│ Manual Review │
│ Some vulnerabilities require your attention to resolve │
│ │
│ Visit https://go.npm.me/audit-guide for additional guidance │
└──────────────────────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ High │ Prototype Pollution │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package │ lodash │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in │ >=4.17.11 │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ deps-ok │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path │ deps-ok > lodash │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info │ https://npmjs.com/advisories/782 │
└───────────────┴──────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ High │ Prototype Pollution │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package │ lodash │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in │ >=4.17.12 │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ deps-ok │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path │ deps-ok > lodash │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info │ https://npmjs.com/advisories/1065 │
└───────────────┴──────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ Low │ Prototype Pollution │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package │ lodash │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in │ >=4.17.19 │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ deps-ok │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path │ deps-ok > lodash │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info │ https://npmjs.com/advisories/1523 │
└───────────────┴──────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ Low │ Prototype Pollution │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package │ minimist │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in │ >=0.2.1 <1.0.0 || >=1.2.3 │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ deps-ok │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path │ deps-ok > minimist │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info │ https://npmjs.com/advisories/1179 │
└───────────────┴──────────────────────────────────────────────────────────────┘
found 4 vulnerabilities (2 low, 2 high) in 13 scanned packages
4 vulnerabilities require manual review. See the full report for details.
现在你当然可能不关心这些漏洞,因为这个脚本只在构建期间使用,但现在你有一个新问题,任何真正的漏洞都将隐藏在这些你不关心的漏洞中。
我不知道上面的解决方案有多强大或全面。好消息是它依赖于npm,所以无论npm install 做什么,它都会做同样的事情。