How to easily verify correct npm dependencies installed?

后端 未结 6 934
北海茫月
北海茫月 2020-12-07 01:52

How can I know when to prompt user to run npm install if there are any unmet package.json dependencies?

I would like to do this, because if any re

相关标签:
6条回答
  • 2020-12-07 02:21

    Found this today. Not sure if your still need this.

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

    npm install check-dependencies --save-dev
    

    Install this package and save to your package.json.

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

    config is the following object, which is then passed to the callback.

    {
        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
    }
    
    0 讨论(0)
  • Not sure there is npm way to do it, but just found this seems helpful:

    blog post: http://bahmutov.calepin.co/check-dependencies-in-grunt-by-default.html

    project: https://github.com/bahmutov/deps-ok

    0 讨论(0)
  • 2020-12-07 02:27

    You can use yarn and do yarn check --verify-tree (you can continue using npm for everything else)

    0 讨论(0)
  • 2020-12-07 02:29

    If you use github, or at least host your project.json on github, you can use: https://david-dm.org/

    replace your username and repo.

    https://david-dm.org/username/repo.svg
    

    Then you can see something like https://david-dm.org/bower/bower to identify the out of date packages.

    0 讨论(0)
  • 2020-12-07 02:34

    npm ls will report missing packages when run from the project folder.

    npm-ls documentation

    This might have issues if you're using git dependencies, though. (Thanks @gman).

    0 讨论(0)
  • 2020-12-07 02:39

    Another solution

    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;
    }
    

    Call it like this

    if (dependenciesNeedUpdating()) {
      console.error('dependencies need updating. Please run `npm install`');
      process.exit(1);
    }
    

    If you want to install this as a dependency

    0 讨论(0)
提交回复
热议问题