Access Package.json Data via Gulpfile

Much of development seems to consist of wiring together files/data/languages/etc/etc. While trivially easy in many cases, learning how isn’t always obvious.

Here’s an example of how to access a json-key’s value from a package.json file inside of a gulpfile, useful for versioning shippable packages.

Package.json

{
  "version": "1.0.2",
  ...
}

gulpfile.js

// Variablize access to package.json by requiring it:

var packageJSON = require('./package.json')

// Now we can access any json-key value by calling
// packageJSON.key, where 'key' is the json key. 

// Here's one use case, adding a version number to a shippable package:

gulp.task('build', function() {
    ...
    .pipe(gulp.dest('../' + 'shippable-package-v' + packageJSON.version))
})

// This creates: shippable-package-v1.0.2