Javascript: get package.json data in gulpfile.js

Not any gulp -special issue per se, but how to get information from the package.json file in the gulpfile.js file; For example, I want to get a homepage or name and use it in a task.

+81
javascript gulp
Mar 25
source share
4 answers

Do not use require('./package.json') for the browsing process. Using require allow the module as the result of the first request. Therefore, if you are editing the package.json file, these changes will not work unless you stop the viewing process and restart it. It would be better to use the bitlinguist method for the gulp browsing process, as it will re-read the file and parse it every time your task is performed.

 var fs = require('fs'); var json = JSON.parse(fs.readFileSync('./package.json')); 
+88
Apr 13
source share

This is not a gulp.

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

UPDATE:

Keep in mind that β€œrequire” will cache read results - this means that you cannot require, write to a file, and then require again and expect the results to be updated.

+120
Mar 25 '14 at 21:01
source share

This is a good @Mangled Deutz solution. At first I did it, but it did not work (also after a second), I tried this solution:

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

You should now see that this is a bit detailed (although it does work). require('./package.json') is the best solution:

Tip

- remember to add './' in front of the file name. I know its simple, but the difference is that the require method works and doesn't work.

+5
Feb 03 '15 at 8:21
source share

If you run gulp from NPM, for example, using npm run build "or something

(This only works for gulp triggers using NPM)

process.env.npm_package_Object

this should be separated by an underscore for deeper objects.

if you want to read some specific configuration in package.json how do you want to read the configuration object you created in package.json

 scripts : { build: gulp }, config : { isClient: false. } 

then you can use

 process.env.npm_package_**config_isClient** 
0
Dec 16 '17 at 10:55
source share



All Articles