Is there a way to automatically install node.js dependencies from a .js file?

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

It's called auto-install.

enter image description here


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/