Is there a way to generate the bundledDependencies list automatically?
Asked Answered
A

2

12

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?

Anthracite answered 11/3, 2012 at 2:54 Comment(1)
PING :) Did this answer your question or is there something else to be solved?Mezzo
M
10

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.)

Mezzo answered 14/12, 2012 at 21:57 Comment(1)
took your answer and made a library.. github.com/GuyMograbi/grunt-bundled-dependencies. please consider adding to your answer.Rupiah
S
2

You can use a simple Node.js script to read and update the bundleDependencies property and run it via npm lifecycle hooks/scripts.

My folder structure is:

  • scripts/update-bundle-dependencies.js
  • package.json

scripts/update-bundle-dependencies.js:

#!/usr/bin/env node
const fs = require('fs');
const path = require('path');    
const pkgPath = path.resolve(__dirname, "../package.json");
const pkg = require(pkgPath);
pkg.bundleDependencies = Object.keys(pkg.dependencies);    
const newPkgContents = JSON.stringify(pkg, null, 2);    
fs.writeFileSync(pkgPath, newPkgContents);
console.log("Updated bundleDependencies");

If you are using the latest version of npm (> 4.0.0), you can use the prepublishOnly or prepack script: https://docs.npmjs.com/misc/scripts

prepublishOnly: Run BEFORE the package is prepared and packed, ONLY on npm publish. (See below.)

prepack: run BEFORE a tarball is packed (on npm pack, npm publish, and when installing git dependencies)

package.json:

{
  "scripts": {
    "prepack": "npm run update-bundle-dependencies",
    "update-bundle-dependencies": "node scripts/update-bundle-dependencies"
  }
}

You can test it yourself by running npm run update-bundle-dependencies.

Hope this helps!

Santoyo answered 18/1, 2018 at 20:47 Comment(1)
isn't it bundledDependencies instead of pkg.bundleDependencies = Object.keys(pkg.dependencies);Piggin

© 2022 - 2024 — McMap. All rights reserved.