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');
2 Likes

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!

I just tried both require error: ReferenceError: require is not defined and fs.readFileSync (fs is not defined).

Is there a way to load the contents of a separate file from within the package.js code?

@permb
do you have ‘ecmascript’ under api.use()?

Yup.

Some of the packages published by the MeteorPackaging project did things like this. One example is the package files for Bootswatch: GitHub - MeteorPackaging/bootswatch: Bootswatch Bootstrap themes, all in one repo. It seems this is missing from Meteor’s docs.

I might be wrong here… If you depened on the ecmascript package in a package, you can only use import and cannot use require.

require can always be used in modules. Meteor’s module system is closer in design to Webpack or Bun than Node.

package.js is not loaded as a module and instead is run similar to how build plugins or packages are run when they do not have a dependency on the modules package. They can use Npm.require though.