简体   繁体   English

如何确保在NodeJS中加载了模块

[英]How to make sure a module is loaded in NodeJS

This is a problem I faced more than one. 这是我面临的一个以上的问题。 Here is an example file structure: 这是一个示例文件结构:

app.js
folder
-- index.js
-- module1.js
-- module2.js

From app.js , the entry point of my application, I require folder/index.js . app.js (我的应用程序的入口点),我需要folder/index.js This file itself is just a loader who requires all the other files in the directory. 该文件本身只是一个加载器,它需要目录中的所有其他文件。 module1.js and module2.js both define some methods I want to use eventually. module1.jsmodule2.js都定义了我最终要使用的一些方法。 They are never directly required by app.js since index.js takes care of that and adds common helper utilities and applies some transformations to these modules. app.js从未直接需要它们,因为index.js会处理这些问题并添加通用的帮助程序实用程序并将一些转换应用于这些模块。

All works well if I then use some methods defined in those files from app.js after I required them. 如果我在需要后使用了app.js文件中定义的某些方法,则一切正常。 But the problem comes when a method defined in module2.js wants to use a method defined in method1.js (or vice-versa). 但是问题是当module2.js定义的方法想要使用method1.js定义的方法时method1.js (反之亦然)。 Sometimes it will work, sometimes not (in folders with multiple files I didn't get to find out when precisely it works and when it doesn't yet). 有时它会起作用,有时却不起作用(在包含多个文件的文件夹中,我没有弄清楚它何时确切地起作用以及何时不起作用)。

If, from module2.js I require ./index.js and then use methods in it defined by module1.js , I sometimes get Cannot read property 'myMethod' of undefined . 如果从module2.js我需要./index.js ,然后使用由module1.js定义的方法,则有时会得到Cannot read property 'myMethod' of undefined I assume it has to do with the order the modules are required by index.js , but I can't seem to find a solution to this common problem (other than duplicating code or not using methods of these other modules). 我认为这与index.js所需模块的顺序有关,但是我似乎找不到解决此常见问题的解决方案(除了复制代码或不使用其他模块的方法)。

Is there a common solution to this problem? 有解决这个问题的通用方法吗? I tried doing something like this : 我试图做这样的事情:

var modules = require(./index.js);

exports.myMethod = function() {
  if(!modules.module1 || !modules.module1.myOtherMethod) {
    modules = require('./index.js');
  }
  modules.module1.myOtherMethod();
};

But it doesn't to do anything, modules.module1 is still undefined. 但是它什么也没做, modules.module1仍然是未定义的。

It just sounds like module should require module2 . 听起来module应该require module2 That's the solution to module1 needing to call methods in module2 . 这是module1需要调用module2方法的解决方案。

If you're worried about many calls to require , don't. 如果您担心require致电很多,请不要。 That's standard practice in every programming language I've used (in particular look at the import statements in any Java program. Java is verbose, I know, but this is correct practice.) 这是我使用过的每种编程语言的标准做法(特别是查看任何Java程序中的import语句。我知道Java很冗长,但这是正确的做法。)

It's definitely bad practice to look at code in one module and have no idea where anything comes from, because the require statements are missing. 在一个模块中查看代码,不知道任何东西来自哪里,这绝对是错误的做法,因为require语句丢失了。

You have a circular dependency problem. 您有循环依赖问题。 Try moving some of the common functions to a third file and have module1 and module2 require it, or make sure that one of them requires the other in one way only. 尝试将一些常用功能移至第三个文件,并让module1module2需要它,或者确保其中一个仅以一种方式需要另一个。

Never ever require a file that requires the current file back. 永远不需要一个需要返回当前文件的文件。

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

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