javascript 有没有办法从 .js 文件自动安装 node.js 依赖项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22125022/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:30:21  来源:igfitidea点击:

Is there a way to automatically install node.js dependencies from a .js file?

javascriptnode.jsdependenciesnpm

提问by sivano

If I have a foo.js node script, is there a way for me to automatically install all the npm dependencies?

如果我有一个 foo.js 节点脚本,有没有办法让我自动安装所有 npm 依赖项?

e.g. If foo.js had this:

例如,如果 foo.js 有这个:

var program = require('commander');
var cheerio = require('cheerio');

Is there any npm command or something that I could do that would read foo.js and do 'npm install commander;npm install cheerio'?

是否有任何 npm 命令或我可以做的事情来读取 foo.js 并执行“npm install command;npm installcheerio”?

回答by xbonez

List your dependencies in a package.jsonfile. You can then run npm installto install all dependencies.

package.json文件中列出您的依赖项。然后,您可以运行npm install以安装所有依赖项。

Here's an example of a package.json file. Notice how dependencies are defined:

这是 package.json 文件的示例。注意依赖项是如何定义的:

{
  "name": "best-practices",
  "description": "A package using versioning best-practices",
  "author": "Charlie Robbins <[email protected]>",
  "dependencies": {
    "colors": "0.x.x",
    "express": "2.3.x",
    "optimist": "0.2.x"
  },
  "devDependencies": {
    "vows": "0.5.x"
  },
  "engine": "node >= 0.4.1"
}

Source: https://blog.nodejitsu.com/package-dependencies-done-right/

来源:https: //blog.nodejitsu.com/package-dependencies-done-right/

回答by Christian Maioli M.

There is now a tool that auto-installs required dependencies as you code.

现在有一个工具可以在您编写代码时自动安装所需的依赖项。

It's called auto-install.

这称为自动安装

enter image description here

在此处输入图片说明

回答by Luca C.

npm-install-peers is a npm package that will detect peers and install them.

npm-install-peers 是一个 npm 包,它将检测对等点并安装它们。

Note that you should install it globally

请注意,您应该全局安装它