简体   繁体   中英

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?

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

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

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

okay, but how can I use nodemon and auto-install together?

thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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