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 to automatically install all npm dependencies?

eg. If foo.js had the following:

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

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

+7
javascript npm dependencies
source share
3 answers

List your dependencies in the package.json file. Then you can run npm install to install all the dependencies.

Here is an example package.json file. Notice how the dependencies are determined:

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

+7
source share

Now there is a tool that automatically installs the necessary dependencies when encoding.

It is called auto-install .

enter image description here

+3
source share

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

Please note that you must install it globally

0
source share

All Articles