有没有办法在nodejs中“只需要”一次JS文件?

时间:2021-10-24 09:35:19

I have just started working with nodejs. I wonder if there is a way to "require" a file only once in an app. I am using a class framework for getting classic OOPS in my JS project. Each "class" is contained in its own JS file. I want to "require" the class framework in each file so that they can function independently but want the framework's init code to be executed only once.

我刚开始使用nodejs。我想知道是否有办法在应用程序中“只需要”一次文件。我正在使用类框架来获取我的JS项目中的经典OOPS。每个“类”都包含在自己的JS文件中。我想“要求”每个文件中的类框架,以便它们可以独立运行,但希望框架的init代码只执行一次。

I can use a flag to implement this myself but a built-in way would be nice. Search for "require once" leads me to all PHP related questions.

我可以使用一个标志来自己实现,但内置的方式会很好。搜索“require once”会引导我查看所有与PHP相关的问题。

2 个解决方案

#1


66  

require is always "require once". After you call require the first time, require uses a cache and will always return the same object.

要求总是“需要一次”。在第一次调用require后,require使用缓存并始终返回相同的对象。

Any executable code floating around in the module will only be run once.

在模块中浮动的任何可执行代码只会运行一次。

On the other hand, if you do want it to run initialisation code multiple times, simply throw that code into an exported method.

另一方面,如果您确实希望它多次运行初始化代码,只需将该代码抛出到导出的方法中即可。

edit: Read the 'Caching' section of http://nodejs.org/docs/latest/api/modules.html#modules

编辑:阅读http://nodejs.org/docs/latest/api/modules.html#modules的“缓存”部分

#2


1  

If you really want the top level code in your module (code that is not contained within methods or functions in your module) to execute more than once you can delete it's module object that is cached on the require.cache object like this:

如果你真的想要模块中的*代码(模块中没有包含在方法或函数中的代码)执行多次,你可以删除它在require.cache对象上缓存的模块对象,如下所示:

delete require.cache[require.resolve('./mymodule.js')];

Do this before you require the module for the second time.

在您第二次需要该模块之前执行此操作。

Most of the time though you probably only want the module's top level code to run once and any other time you require the module you only want to access what that module exports.

大多数情况下,您可能只希望模块的*代码运行一次,而您需要模块的任何其他时间只需要访问该模块导出的内容。

var myMod = require("./mymodule.js"); //the first time you require the
                                      //mymodule.js module the top level code gets
                                      //run and you get the module value returned.


var myMod = require("./mymodule.js"); //the second time you require the mymodule.js
                                      //module you will only get the module value
                                      //returned. Obviously the second time you
                                      //require the module it will be in another
                                      //file than the first one you did it in.

#1


66  

require is always "require once". After you call require the first time, require uses a cache and will always return the same object.

要求总是“需要一次”。在第一次调用require后,require使用缓存并始终返回相同的对象。

Any executable code floating around in the module will only be run once.

在模块中浮动的任何可执行代码只会运行一次。

On the other hand, if you do want it to run initialisation code multiple times, simply throw that code into an exported method.

另一方面,如果您确实希望它多次运行初始化代码,只需将该代码抛出到导出的方法中即可。

edit: Read the 'Caching' section of http://nodejs.org/docs/latest/api/modules.html#modules

编辑:阅读http://nodejs.org/docs/latest/api/modules.html#modules的“缓存”部分

#2


1  

If you really want the top level code in your module (code that is not contained within methods or functions in your module) to execute more than once you can delete it's module object that is cached on the require.cache object like this:

如果你真的想要模块中的*代码(模块中没有包含在方法或函数中的代码)执行多次,你可以删除它在require.cache对象上缓存的模块对象,如下所示:

delete require.cache[require.resolve('./mymodule.js')];

Do this before you require the module for the second time.

在您第二次需要该模块之前执行此操作。

Most of the time though you probably only want the module's top level code to run once and any other time you require the module you only want to access what that module exports.

大多数情况下,您可能只希望模块的*代码运行一次,而您需要模块的任何其他时间只需要访问该模块导出的内容。

var myMod = require("./mymodule.js"); //the first time you require the
                                      //mymodule.js module the top level code gets
                                      //run and you get the module value returned.


var myMod = require("./mymodule.js"); //the second time you require the mymodule.js
                                      //module you will only get the module value
                                      //returned. Obviously the second time you
                                      //require the module it will be in another
                                      //file than the first one you did it in.