adapt-dev adapt-dev - 6 months ago 11
Node.js Question

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

I would like to get at a

.json
file in one of my dependent libraries.

using fs to read
./node_modules/somelib/properties.json
doesn't seem foolproof because
somelib
could have been installed globally.

Can this be done with
require
?

Answer

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.