node.jsnpm

How to easily verify correct npm dependencies installed?


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 require() fails, the user gets a poor error message:

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

I've previously tried to just check for the existence of a node_modules directory, but this only works effectively for fresh git clones. I've also tried just requiring npm and running npm install as part of load, but that is very heavy weight.

I'm hoping there is a lighter weight library out there that just parses package.json and makes sure node_modules contents satisfy the requirements.

One idea was to use process.on('uncaughtException') to catch only module import errors, but looking to see if there is a "standard" solution first.


Solution

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