简体   繁体   English

需要全局 scope 或本地 scope?

[英]Require in global scope or local scope?

What is the correct way to require a node module?要求节点模块的正确方法是什么? Is it more accurate/understandable to declare the module in the global scope, or is it more accurate/understandable to declare the module in the local scope?在全局 scope 中声明模块更准确/更易于理解,还是在本地 scope 中声明模块更准确/更易于理解?

For example, which of these makes the most sense:例如,以下哪个最有意义:

Global:全球的:

let dns = require('dns') // <-- Global scope declaration
function lookup(subdomain, domain){
    let fqdn
    if(subdomain == '@'){
        fqdn = domain
    } else {
        fqdn = `${subdomain}.${domain}`
    }
    dns.resolveTxt(fqdn, (err, records) => {
        if(records){
            console.log(records)
        } else {
            console.log("no recrods")
        }
    })
}

Local:当地的:

function lookup(subdomain, domain){
    let dns = require('dns') // <-- Local scope declaration
    let fqdn
    if(subdomain == '@'){
        fqdn = domain
    } else {
        fqdn = `${subdomain}.${domain}`
    }
    dns.resolveTxt(fqdn, (err, records) => {
        if(records){
            console.log(records)
        } else {
            console.log("no recrods")
        }
    })
}

Is it a matter of opinion?这是意见问题吗? If so, my apologies and I will delete the question.如果是这样,我很抱歉,我将删除这个问题。

I am looking to improve my code so others can more easily understand it, and I think this question is relevant to that purpose, so it shouldn't be considered an opinionated question.我希望改进我的代码,以便其他人更容易理解它,我认为这个问题与该目的相关,因此不应将其视为一个固执己见的问题。

First, a minor correction.首先,小幅修正。 Your first example is not global scope.您的第一个示例不是全局 scope。 That's module scope.那是模块 scope。 Module scope with the declaration located at the beginning of your module file is the preferred implementation for the following reasons:带有位于模块文件开头的声明的模块 scope 是首选实现,原因如下:

  1. It loads at startup (see text below for why that's good).它在启动时加载(请参阅下面的文字了解为什么这样做很好)。
  2. Locating all these at the beginning of your module file clearly states in the code what external module dependencies you have in a nice easy to see way which is useful for anyone coming back to this code in the future to do maintenance (including you).将所有这些放在模块文件的开头清楚地在代码中以一种易于查看的方式清楚地说明了您拥有哪些外部模块依赖项,这对于将来返回此代码进行维护的任何人(包括您)都很有用。
  3. You only ever require() this module once in this module.你在这个模块中只require()这个模块。 While modules are cached, it's better to not be calling require() every time you want to reference it.虽然缓存了模块,但最好不要在每次要引用它时都调用require() Load it once and use that saved module reference.加载一次并使用保存的模块引用。

About point 1 above, require() is blocking and synchronous and involves accessing the file system (the first time that file is loaded).关于上面的第 1 点, require()是阻塞和同步的,并且涉及访问文件系统(第一次加载该文件时)。 You generally want to get all your require() operations done at server-startup so no blocking operations are happening while you are processing requests.您通常希望在服务器启动时完成所有require()操作,因此在处理请求时不会发生阻塞操作。 And, if you get any errors from require() (such as a module install missing or version conflict or bad configuration or missing dependency or something like that), you also want those to happen at startup time, not later where they are quicker to see and easier to diagnose.而且,如果您从require()中得到任何错误(例如模块安装丢失或版本冲突或错误配置或缺少依赖项或类似的东西),您还希望这些在启动时发生,而不是稍后它们更快地发生看到并更容易诊断。

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

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