node.js 可以为节点应用程序安装所有缺少的模块吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13189239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Possible to install all missing modules for a node application?
提问by Abe Miessler
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?
我有一个刚开始使用的节点应用程序,每次尝试运行它时,它都说缺少一个模块。我刚刚npm install ...为每个模块使用过,但是在完成了大约 10 个模块之后,我想知道是否有一种方法可以让 npm 为节点应用程序下拉所有需要的模块,而无需我手动安装每个模块。可以做到吗?
回答by JP Richardson
Yes, as long as the dependency is listed in package.json.
是的,只要依赖项在package.json.
In the directory that contains package.json, just type:
在包含 的目录中package.json,只需键入:
npm install
回答by alexcline
I created an npm module to handle installing missing modules automatically.
我创建了一个 npm 模块来自动处理安装丢失的模块。
It will install all app dependencies and sub-dependencies automatically. This is useful when submodules aren't installed correctly.
它将自动安装所有应用程序依赖项和子依赖项。当子模块未正确安装时,这很有用。
回答by Renato Gama
You can run npm install yourModule --savein order to install and automatically update package.jsonwith this newly installed module.
您可以运行npm install yourModule --save以安装并自动更新package.json这个新安装的模块。
So when you run npm installa second time it will install every dependecy previously added and you won't need to reinstall every dependency one by one.
因此,当您npm install第二次运行时,它会安装之前添加的每个依赖项,您无需一一重新安装每个依赖项。
回答by Aminadav Glickshtein
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)
}
}
})
}
}
})()
回答by Gust van de Wal
I was inspired by @Aminadav Glickshtein's answerto create a script of my own that would synchronouslyinstall the needed modules, because his answer lacks these capabilities.
我受到@Aminadav Glickshtein 的回答的启发,我创建了一个自己的脚本来同步安装所需的模块,因为他的回答缺乏这些功能。
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:
我需要一些帮助,所以我在这里开始了一个 SO 问题。您可以在那里阅读有关此脚本如何工作的信息。
结果如下:
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-catchand doesn't log anything in the console:
还有一个单行(139 个字符!)。它没有全局定义child_modules,没有 lasttry-catch并且不会在控制台中记录任何内容:
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()

