简体   繁体   English

Node.js - 如何获得相对需要的路径?

[英]Node.js - How to get relative require paths?

I have a module that works like so:我有一个像这样工作的模块:

/helpers/importer/Import.js /helpers/importer/Import.js

function importer(requiredModulePath, toImport) {
  requiredModulePath = requiredModulePath.replace('{env}', browser.params.env);
  var requiredModule = require(requiredModulePath);

  return requiredModule[toImport] || requiredModule.default || requiredModule;
}

module.exports = importer;

I use this to import module properties dynamically, such as the following "URLs" module:我使用它来动态导入模块属性,例如以下“URL”模块:

/example/URLs.js /示例/URLs.js

module.exports = {
  default: 'http://google.com',
  a: 'http://a.com',
  b: 'http://c.com'
};

And I do this like so:我这样做:

/example/myExecutingModule.js /示例/myExecutingModule.js

var Import = require('../helpers/importer/Import');
//var URL = Import('./URLs');             //This path does not work
var URL = Import('../../example/URLs');   //This path does work, as the URL is relative to Import.js 
[ ... ]

My problem here is that, unlike everyone else on stack overflow, who wants absolute paths in their requires, I want the opposite.我的问题是,与堆栈溢出的其他人不同,他们希望在他们的需求中使用绝对路径,我想要相反的。 I would like to supply the same path to my Import function that I would supply to require -- the one that is relative to myExecutingModule.js, in this case: './URLs'.我想为我的导入函数提供与我需要的相同的路径——与 myExecutingModule.js 相关的路径,在这种情况下:'./URLs'。 However, it seems like my "importer" function has closure over the "require" function which is relative to its path.但是,似乎我的“importer”函数对与其路径相关的“require”函数有闭包。 Is there any way to write it such that "require" will be relative to the module calling importer()?有什么方法可以编写它,使“require”与调用 importer() 的模块相关? As I type this, it feels like the only way would be to pass "require" into the Import.js module, which I feel is kind of... ridiculous当我输入这个时,感觉唯一的方法是将“require”传递到 Import.js 模块中,我觉得这有点......荒谬

EDIT: After thinking on this further I am guessing that the answer involves giving Import closure over the calling module's require function, like so:编辑:在进一步考虑之后,我猜测答案涉及在调用模块的 require 函数上提供导入闭包,如下所示:

var Import = require('../helpers/Import')(require);

And then the export in Import.js will be然后 Import.js 中的导出将是

module.exports = function (require) { [...] return importer; };

Causing "require" within the importer function to be the actual require function that the calling module would have used.导致导入器函数中的“require”成为调用模块将使用的实际 require 函数。 I have not tried this yet, and I'm holding out for a better answer, so I will leave this question open for now我还没有尝试过这个,我正在等待一个更好的答案,所以我现在将这个问题保持开放

您可以使用__dirname设置为当前模块所在的目录。例如: __dirname + '/URLs'

Try the local-modules package.试试local-modules包。 I've used it in production on node 0.10 and io.js 2.3 so far without any problems.到目前为止,我已经在 node 0.10 和 io.js 2.3 的生产中使用它,没有任何问题。

https://www.npmjs.com/package/local-modules https://www.npmjs.com/package/local-modules

https://github.com/intesso/local-modules https://github.com/intesso/local-modules

This is the solution I am using now.这是我现在使用的解决方案。 Passing "require" to the Import module from the calling module, so that it is importing relative to the calling module's location.将“require”从调用模块传递给导入模块,以便它相对于调用模块的位置进行导入。

var import = require('../helpers/Import')(require);
var URL = import('./URLs');

And then Import.js uses the calling module's require instead of its own:然后 Import.js 使用调用模块的 require 而不是它自己的:

module.exports = function (require) {
  function importer(requiredModulePath) {

    [ ... ]

    var requiredModule = require(requiredModulePath);
    return requiredModule[toImport] || requiredModule.default || requiredModule;
  }

  return importer;
};

Feels a little weird but, what can ya do感觉有点奇怪但是你能做什么

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

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