简体   繁体   中英

In Node.js, how can a module get data from an application's package.json?

I have a module. Inside it, I'd like to access data from my parent application's package.json file. What's the best practice way to do that?

I've done this the janky way by going up 2 levels and requiring the file (or using the nconf configuration loader).

var appdir = path.resolve(__dirname, '../../');
nconf.file('app', path.join(appdir, 'package.json'));

But that seems like it could break easily.

Also I heard about pkginfo , it will automatically grab info from my own module's package.json, but I'm looking to get data from the parent application's.

Thanks for any help!

EDIT: I suppose another way of asking is, how can I get the application's path (instead of the module path) ?

You can use

require.main.require './package'

But it'll work only if your parent application's file is in a root of it's directory.

You can also wrap it into try ... catch and add ../ to path till you find package.json .

You can read more about accessing main module here http://nodejs.org/api/modules.html#modules_accessing_the_main_module

Adding to pcru's approach, this should work:

function loadMainPackageJSON(attempts) {
  attempts = attempts || 1;
  if (attempts > 5) {
    throw new Error('Can\'t resolve main package.json file');
  }
  var mainPath = attempts === 1 ? './' : Array(attempts).join("../");
  try {
    return require.main.require(mainPath + 'package.json');
  } catch (e) {
    return loadMainPackageJSON(attempts + 1);
  }
}

var packageJSON = loadMainPackageJSON();

Keep in mind that this will get you the main module, which something you think is what you want , but if you're building a command line tool like I was, what you really want is to get the package.json exactly two folders above where you were installed if your tool is meant to be installed locally and called with npm run-script

As mentioned in Determine project root from a running node.js application , one of the easiest ways to get to your application path is using process.cwd() , provided you have the discipline to always start your main js program from the same directory. ie node app/main.js and cd app && node main.js will give different results.

Another way could be to recursively follow the references to module.parent , then read out module.filename when module.parent is undefined. This also presupposes some knowledge about your app. It won't work if the location of the main script relative to the package.json could vary. Ie you must know if the main script is in the root of the app directory, or maybe in some sort of 'bin', 'app', or 'lib' dir. However, once you find the top-level module, you could try to locate the closest package.json using the same algorithm pkginfo uses to find the package.json for the current file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM