Load json file from package.js?

Is it possible to load a json file (say package.json) from package.js? For build plugins, the npm requirements have to be hard coded - which is annoying. If there was some way to load content from the package.json file, we could just defer to whatever is in there.

I’m pretty sure I’ve loaded fs and run readFileSync inside a package.js before. So I think it’s possible, just need to work out the correct path when it’s being read

You should be able to just use require.

const vars = require('./package.json');
1 Like

That sounds much simpler and more obvious than fs.readFileSync :laughing:

require was undefined in package.js. I would need to find the path anyway. I ended up putting the necessary code elsewhere (using findUp) and it worked well!