15

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

2

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer and useful link, let me read up on it! I was (naively?) hoping there was a programmatic way to extract the dependencies from the .js file itself. I'll read through the package dependencies link you posted.
9

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

It's called auto-install.

enter image description here

Comments

0

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

Note that you should install it globally

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.