46

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 module but after doing about 10 of them I'm wondering if there is a way to have npm pull down all needed modules for a node app without me installing each one manually. Can it be done?

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • you should use https://github.com/imkimchi/ni, It automatically install missing dependencies from package.json comparing all js codes in the project – Phillip YS Aug 30 '18 at 09:37

5 Answers5

84

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

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

npm install
JP Richardson
  • 38,609
  • 36
  • 119
  • 151
16

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.

alexcline
  • 450
  • 4
  • 9
4

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.

Renato Gama
  • 16,431
  • 12
  • 58
  • 92
1

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)
          }
        }
      })
    }
  }
})()
Gust van de Wal
  • 5,211
  • 1
  • 24
  • 48
Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
1

I was inspired by @Aminadav Glickshtein's answer to create a script of my own that would synchronously install the needed modules, because his answer lacks these capabilities.

I needed some help, so I started an SO question here. You can read about how this script works there.
The result is as follows:

const cp = require('child_process')

const req = async module => {
  try {
    require.resolve(module)
  } catch (e) {
    console.log(`Could not resolve "${module}"\nInstalling`)
    cp.execSync(`npm install ${module}`)
    await setImmediate(() => {})
    console.log(`"${module}" has been installed`)
  }
  console.log(`Requiring "${module}"`)
  try {
    return require(module)
  } catch (e) {
    console.log(`Could not include "${module}". Restart the script`)
    process.exit(1)
  }
}

const main = async () => {
  const http    = await req('http')
  const path    = await req('path')
  const fs      = await req('fs')
  const express = await req('express')

  // The rest of the app's code goes here
}

main()

And a one-liner (139 characters!). It doesn't globally define child_modules, has no last try-catch and doesn't log anything in the console:

const req=async m=>{let r=require;try{r.resolve(m)}catch(e){r('child_process').execSync('npm i '+m);await setImmediate(()=>{})}return r(m)}

const main = async () => {
  const http    = await req('http')
  const path    = await req('path')
  const fs      = await req('fs')
  const express = await req('express')

  // The rest of the app's code goes here
}

main()
Gust van de Wal
  • 5,211
  • 1
  • 24
  • 48