admin管理员组

文章数量:1184762

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

e.g. If foo.js had this:

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'?

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

e.g. If foo.js had this:

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'?

Share Improve this question edited Mar 2, 2014 at 7:30 Matt Ball 360k102 gold badges653 silver badges719 bronze badges asked Mar 2, 2014 at 7:27 sivanosivano 7291 gold badge6 silver badges12 bronze badges 2
  • to install the dependencies automatically , first of all list them manually in package.json file and run the npm install(sometimes sudo npm install) command. – Lekhnath Commented Mar 2, 2014 at 7:54
  • Possible duplicate of Is it possible to automatically install the required modules for a node.js script? – Anderson Green Commented Sep 10, 2016 at 1:17
Add a comment  | 

3 Answers 3

Reset to default 15

List your dependencies in a package.json file. You can then run npm install to install all dependencies.

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

{
  "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/

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.

Note that you should install it globally

本文标签: javascriptIs there a way to automatically install nodejs dependencies from a js fileStack Overflow