简体   繁体   English

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

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

If I have a foo.js node script, is there a way for me to automatically install all the npm dependencies?如果我有一个 foo.js 节点脚本,有没有办法让我自动安装所有 npm 依赖项?

eg 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”?

List your dependencies in a package.json file.package.json文件中列出您的依赖项。 You can then run npm install to install all dependencies.然后,您可以运行npm install来安装所有依赖项。

Here's an example of a package.json file.这是 package.json 文件的示例。 Notice how dependencies are defined:注意依赖项是如何定义的:

{
  "name": "best-practices",
  "description": "A package using versioning best-practices",
  "author": "Charlie Robbins <charlie@nodejitsu.com>",
  "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/

There is now a tool that auto-installs required dependencies as you code.现在有一个工具可以在您编写代码时自动安装所需的依赖项。

It's called auto-install .这称为自动安装

在此处输入图片说明

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请注意,您应该全局安装它

okay, but how can I use nodemon and auto-install together? 好的,但是如何一起使用nodemon和自动安装?

thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM