Skip to content

Instantly share code, notes, and snippets.

@tamlyn
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamlyn/4f8fda4975ee5ec2c527 to your computer and use it in GitHub Desktop.
Save tamlyn/4f8fda4975ee5ec2c527 to your computer and use it in GitHub Desktop.
Warn when Gulp project dependencies need updating

How many times have you had this conversation?

"Hey, the project doesn't run on my machine any more."

"Have you tried running npm install?"

...pause...

"Yeah, that fixed it, thanks"

This is a neat trick that shows a warning in the console when starting up a project if the dependencies are out of date.

It does this by comparing the modified time of package.json with the modified time of node_modules. However that on its own isn't enough because running npm install doesn't necessarily update the file modified time of the folder. Adding a simple touch node_modules command as a postinstall task to package.json fixes this.

Add the check-deps task as a dependency of your default gulp task so it always runs when starting up a project. Depending how idiot proof you wanna get, you could even add it as a post-update hook to your git checkout.

var fs = require('fs');
var gulp = require ('gulp');
var util = require('gulp-util');
/**
* Display a warning if package.json is more recently modified than node_modules
*/
gulp.task('check-deps', function (cb) {
fs.stat('package.json', function (err, fileStats) {
fs.stat('node_modules', function (err, dirStats) {
if (fileStats.mtime > dirStats.mtime) {
util.log(
util.colors.yellow('Warning'),
'Dependencies may be out of date. Run',
util.colors.blue('npm install'),
'if you encounter errors.'
);
}
cb();
})
});
});
{
"name": "my-app",
"version": "0.1.0",
"devDependencies": {
"gulp": "^3.8.10",
"gulp-util": "^3.0.0"
},
"dependencies": {},
"scripts": {
"postinstall": "touch node_modules"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment