Javascript: get package.json data in gulpfile.js Javascript: get package.json data in gulpfile.js javascript javascript

Javascript: get package.json data in gulpfile.js


This is not gulp specific.

var p = require('./package.json')p.homepage

UPDATE:

Be aware that "require" will cache the read results - meaning you cannot require, write to the file, then require again and expect the results to be updated.


Don't use require('./package.json') for a watch process, as using require will resolve the module as the results of the first request.

So if you are editing your package.json those edits won't work unless you stop your watch process and restart it.

For a gulp watch process it would be best to re-read the file and parse it each time that your task is executed, by using node's fs method

var fs = require('fs')var json = JSON.parse(fs.readFileSync('./package.json'))


This is a good solution @Mangled Deutz. I myself first did that but it did not work (Back to that in a second), then I tried this solution:

# Gulpfile.coffeerequireJSON = (file) ->    fs = require "fs"    JSON.parse fs.readFileSync file

Now you should see that this is a bit verbose (even though it worked). require('./package.json') is the best solution:

Tip

-remember to add './' in front of the file name. I know its simple, but it is the difference between the require method working and not working.