Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Is there a way to generate the bundledDependencies list automatically?

Is there a way to generate the bundledDependencies list automatically?

Problem

I have an app that I'm deploying to Nodejitsu. Recently, they suffered from npm issues that caused my app to go offline for several hours after I tried (and failed) to restart it, as its Dependencies could not be installed. I was told that this could be averted in the future by listing all of my dependencies as bundledDependencies in my package.json, causing the dependencies to be uploaded along with the rest of the application. Which means that I need my package.json to look something like this:

"dependencies": {
  "express": "2.5.8",
  "mongoose": "2.5.9",
  "stylus": "0.24.0"
},
"bundledDependencies": [
  "express",
  "mongoose",
  "stylus"
]

Now, on DRY grounds, this is unappealing. But what's worse is the maintenance: Every time I add or remove a dependency, I have to make the change in two places. Is there a command I can use to sync bundledDependencies with dependencies?

Problem courtesy of: Trevor Burnham

Solution

How about implementing a grunt.js task to do it? This works:

module.exports = function(grunt) {

  grunt.registerTask('bundle', 'A task that bundles all dependencies.', function () {
    // "package" is a reserved word so it's abbreviated to "pkg"
    var pkg = grunt.file.readJSON('./package.json');
    // set the bundled dependencies to the keys of the dependencies property
    pkg.bundledDependencies = Object.keys(pkg.dependencies);
    // write back to package.json and indent with two spaces
    grunt.file.write('./package.json', JSON.stringify(pkg, undefined, '  '));
  });

};

Put that in the root directory of your project in a file called grunt.js. To install grunt, use npm: npm install -g grunt. Then bundle the packages by executing grunt bundle.

A commentor mentioned an npm module that could be useful: https://www.npmjs.com/package/grunt-bundled-dependencies (I haven't tried it.)

Solution courtesy of: wprl

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Is there a way to generate the bundledDependencies list automatically?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×