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
?
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.)
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With