【问题标题】:How to easily verify correct npm dependencies installed?如何轻松验证安装了正确的 npm 依赖项?
【发布时间】:2020-07-15 09:59:42
【问题描述】:

如果存在任何未满足的 package.json 依赖项,我如何知道何时提示用户运行 npm install

我想这样做,因为如果任何require() 失败,用户会收到一条糟糕的错误消息:

module.js:340
    throw err;
          ^
Error: Cannot find module 'nopt'

我之前曾尝试检查是否存在 node_modules 目录,但这仅适用于新的 git 克隆。我也试过只需要 npm 并运行 npm install 作为负载的一部分,但这是非常沉重的。

我希望有一个更轻量级的库,它只解析 package.json 并确保 node_modules 的内容满足要求。

一个想法是使用process.on('uncaughtException') 仅捕获模块导入错误,但首先查看是否有“标准”解决方案。

【问题讨论】:

  • 遍历node_modules,解析他们package.json中的version并与你自己的package.json比较?
  • 没有有效的方法来做到这一点,npm install 负责所有模块依赖项,我看不出它应该如何在你的 npm 包中加载模块时抛出错误。如果这就是您要查找的内容,则有可选的依赖项..
  • grunt-check-dependencies。如果你使用 grunt,你就完成了。如果没有,用 node fs/console 替换 grunt 文件/控制台调用应该不会花很长时间。
  • 我的用例是:我收到运行我的脚本的用户忘记更新 node_modules 的错误报告。我通过npm 自动处理这个包,但这些报告来自使用git clone 测试未发布版本的用户。因此,任何需要节点脚本本身之外的工具的解决方案都将不起作用(即没有咕噜声)。但是,我可以像@jbillich 建议的那样创建一个节点包,也许使用 grunt 脚本作为起点。不过,希望这已经存在。
  • 我也想看这个。

标签: node.js npm


【解决方案1】:

您可以使用yarn 并使用yarn check --verify-tree(您可以继续使用npm 进行其他操作)

【讨论】:

    【解决方案2】:

    今天发现了这个。不知道你是否还需要这个。

    https://www.npmjs.com/package/check-dependencies

    npm install check-dependencies --save-dev
    

    安装这个包并保存到你的 package.json。

    require('check-dependencies')(config, callback);
    

    config 是下面的对象,然后传递给回调。

    {
        status: number,      // 0 if successful, 1 otherwise
        depsWereOk: boolean, // true if dependencies were already satisfied
        log: array,          // array of logged messages
        error: array,        // array of logged errors
    }
    

    【讨论】:

      【解决方案3】:

      npm ls 从项目文件夹运行时会报告丢失的包。

      npm-ls documentation

      不过,如果您使用 git 依赖项,这可能会出现问题。 (感谢@gman)。

      【讨论】:

      • 这实际上失败了。键入 git clone somerepo; cd somerepo; npm install; rm -rf node_modules/somedep; npm ls 不会注意到 dep 丢失。即使只是git clone somerepo; cd somerepo; npm ls 也不会报告任何缺失
      • 这些都不适合我。你能给出你在失败的地方使用的回购吗?我正在使用 npm 6.14.4
      • 看起来它因基于 git 的依赖项而失败。 npm install --save git+https://github.com/someperson/somerepo.git#v1.2.3 现在删除 node_modules/somereponpm ls 不会注意到它已经消失,但 npm install
      【解决方案4】:

      另一种解决方案

      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 做什么,它都会做同样的事情。

      【讨论】:

      • 不错的解决方案。缺点是npm install 需要一段时间才能运行。 deps-ok 速度超级快,您可以在任何脚本之前挂钩它,而不会出现明显的延迟。
      【解决方案5】:

      不确定是否有 npm 方法可以做到这一点,但发现这似乎很有帮助:

      博文:http://bahmutov.calepin.co/check-dependencies-in-grunt-by-default.html

      项目:https://github.com/bahmutov/deps-ok

      【讨论】:

      • 请尽量不要发布仅链接的答案,因为链接的内容可能随时被删除。
      • @DavidPärsson 不确定,我该怎么办?复制项目描述?原来的海报正是这样要求图书馆的。
      • 阅读您的答案时,我不清楚该项目是否解决了问题。我猜第一个链接背后的内容说明了这一点,但该链接似乎已失效。
      • 值得一提的是 archive.org 尝试备份互联网,您可以在他们的 Wayback Machine 上搜索死链接的历史版本。
      • 该死的看起来这个链接实际上已经坏了:bahmutov.calepin.co/check-dependencies-in-grunt-by-default.html 虽然我不同意从链接中复制很多内容,但请给博客作者流量
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 2020-07-04
      • 2016-03-14
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多