Possible to install all missing modules for a node application?

后端 未结 5 592
南旧
南旧 2021-01-30 21:13

I have a node app that I just started working with and each time I try to run it, it says there is a missing module. I\'ve just been using npm install ... for each

5条回答
  •  感动是毒
    2021-01-30 21:32

    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)
              }
            }
          })
        }
      }
    })()
    

提交回复
热议问题