Possible to install all missing modules for a node application?

Solution 1:

Yes, as long as the dependency is listed in package.json.

In the directory that contains package.json, just type:

npm install

Solution 2:

I created an npm module to handle installing missing modules automatically.

npm-install-missing

It will install all app dependencies and sub-dependencies automatically. This is useful when submodules aren't installed correctly.

Solution 3:

You can run npm install yourModule --save in order to install and automatically update package.json with this newly installed module.

So when you run npm install a second time it will install every dependecy previously added and you won't need to reinstall every dependency one by one.

Solution 4:

I have written a script for this.
Place it at the start of your script, and any uninstalled modules will be installed when you run it.

(function () {
  var r = require
  require = function (n) {
    try {
      return r(n)
    } catch (e) {
      console.log(`Module "${n}" was not found and will be installed`)
      r('child_process').exec(`npm i ${n}`, function (err, body) {
        if (err) {
          console.log(`Module "${n}" could not be installed. Try again or install manually`)
          console.log(body)
          exit(1)
        } else {
          console.log(`Module "${n}" was installed. Will try to require again`)
          try{
            return r(n)
          } catch (e) {
            console.log(`Module "${n}" could not be required. Please restart the app`)
            console.log(e)
            exit(1)
          }
        }
      })
    }
  }
})()