如果我有一个foo.js节点脚本,有没有办法让我自动安装所有的npm依赖项?
例如如果foo.js有这个:
var program = require('commander'); var cheerio = require('cheerio');
是否有任何npm命令或我能做的事情会读取foo.js并执行’npm install commander; npm install cheerio’?
解决方法
在
package.json文件中列出您的依赖项.然后,您可以运行npm install来安装所有依赖项.
这是package.json文件的示例.请注意如何定义依赖项:
{ "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" }
资料来源:https://blog.nodejitsu.com/package-dependencies-done-right/