adapt-dev adapt-dev - 1 year ago 55
Node.js Question

Can a .json file be read from a dependent library with require()?

I would like to get at a

file in one of my dependent libraries.

using fs to read
doesn't seem foolproof because
could have been installed globally.

Can this be done with

Answer Source

Yes, this can be done with require.

Require, fetches the file and assigns the exports to the assigned variable. In this case,

var jsonObject = require('./node_modules/somelib/properties.json');

should assign the JSON object to jsonObject variable.