简体   繁体   English

在Node.js中,模块如何从应用程序的package.json中获取数据?

[英]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. 在其中,我想从我父应用程序的package.json文件中访问数据。 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). 我通过上升2级并需要文件(或使用nconf配置加载器)来做到这一点。

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. 我也听说过pkginfo ,它会自动从我自己的模块的package.json中获取信息,但我希望从父应用程序中获取数据。

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 . 你也可以将它包装成try ... catch并添加../到path,直到找到package.json

You can read more about accessing main module here http://nodejs.org/api/modules.html#modules_accessing_the_main_module 您可以在这里阅读有关访问主模块的更多信息http://nodejs.org/api/modules.html#modules_accessing_the_main_module

Adding to pcru's approach, this should work: 添加到pcru的方法,这应该工作:

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 请记住,这将为您提供主要模块,您认为这是您想要的,但如果您正在构建一个像我一样的命令行工具,那么您真正想要的是将package.json正好放在上面的两个文件夹中如果您的工具要在本地安装并使用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. 正如在运行node.js应用程序确定项目根目录中所述,到达应用程序路径的最简单方法之一是使用process.cwd() ,前提是您必须始终从同一目录启动主js程序。 ie node app/main.js and cd app && node main.js will give different results. node app/main.jscd app && node main.js将给出不同的结果。

Another way could be to recursively follow the references to module.parent , then read out module.filename when module.parent is undefined. 另一种方法是递归地跟随对module.parent的引用,然后在未定义module.parent时读出module.filename 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. 如果主脚本相对于package.json的位置可能不同,它将无法工作。 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. 即你必须知道主脚本是在app目录的根目录中,还是在某种'bin','app'或'lib'目录中。 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. 但是,一旦找到顶级模块,就可以尝试使用pkginfo用于查找当前文件的package.json的相同算法找到最近的package.json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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