I want users of my module to be able to customize its behavior by specifying a custom field in package.json but can't figure out how to read it synchronously. If I wanted to read someField out of the package.json of an app or module using my module, how would I do that?
            Asked
            
        
        
            Active
            
        
            Viewed 874 times
        
    1
            
            
         
    
    
        bclinkinbeard
        
- 110
- 6
- 
                    Please, see my answer to a similar question [here][1]. [1]: http://stackoverflow.com/a/22339262/1860357 – jchristin Mar 12 '14 at 00:06
2 Answers
3
            
            
        var pjson = require('./package.json');
console.log(pjson.yourcustomfield);
adopted from another SO question.
- 
                    You can omit the `.json` filename extension if you want so it can be .json, .js, .coffee, etc. – Peter Lyons Nov 21 '13 at 00:55
- 
                    No, that reads it from my own package. If you imagine my package is called `guest`, and it is being used by an app called `host`, I want to access the field defined in host's package.json, form the code running in my guest module. – bclinkinbeard Nov 21 '13 at 01:20
- 
                    Looks like resolving that path gets the right behavior. Anyone see a problem with this? `require(require('path').resolve('./package.json')).someField` – bclinkinbeard Nov 21 '13 at 01:28
- 
                    @bclinkinbeard it looks good, though I would worry about rogue guests that want to ruin my file system. – tomdemuyt Nov 21 '13 at 13:51
1
            
            
        Use fs.readFileSync node.js method of the fs module to read the file synchronously.
 
    
    
        Alex Netkachov
        
- 13,172
- 6
- 53
- 85
 
    