简体   繁体   English

Firebase 功能 | 如果在不同的文件中使用导入会被调用两次吗?

[英]Firebase Functions | Does imports gets called twice if used in different files?

metrics.js指标.js

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

exports.nightlyReport = functions.https.onRequest((request, response) => {
  // ...
});

index.js index.js

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

exports.metrics = require('./metrics');

exports.usageStats = functions.https.onRequest((request, response) => {
  // ...
});

I am importing firebase admin on both index.js and metrics.js .我在index.jsmetrics.js上都导入了 firebase admin。 If I am to call a function from metrics.js , will the firebase admin be imported twice?如果我要从metrics.js调用一个函数,firebase 管理员会被导入两次吗?

No. Requires and imports, by default, are global to the process space and will not duplicate the work of loading and parsing the code.不需要。默认情况下,需求和导入对于进程空间是全局的,不会重复加载和解析代码的工作。 If a second instance is encountered, it will simply return the same reference that were generated the first time.如果遇到第二个实例,它将简单地返回第一次生成的相同引用。

This behavior has nothing to do with Cloud Functions.此行为与 Cloud Functions 无关。 It's just the way things work with nodejs in any environment.这只是在任何环境中使用 nodejs 的方式。

See also:也可以看看:

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

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