Don Rhummy Don Rhummy - 1 year ago 100
Javascript Question

Is there a way to get the package.json properties in a module run from npm?

I'm writing a module will will be run from a "scripts" property like this:

"scripts": {
"runMyModule": "mymodule -stuff stuff"

NOTE: This is not the
of my module. This is the
the user uses to run npm.

I want to allow them to add a property to that main
file (which at the command line they execute by
npm run runMyModule
) which my module will then read. Is this possible?

I want something like:

"forMyModule": {
stuff: [

"scripts": {
"runMyModule": "mymodule -stuff stuff"

Is it possible in my module to get the

I've created both the

Answer Source

Find the root path of the project that uses your module. There are several ways to do this, each with its pros and cons: Determine project root from a running node.js application

Once you have that path, you can simply require(path.join([rootPath, 'package.json'])) and inspect the contents to find the settings.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download