繁体   English   中英

Node.js,如何访问Index.js中导出的模块?

[英]In Node.js, How can I access exported module in Index.js?

我在 stackoverflow 中搜索过,似乎没有像我这样的用例。 我在函数目录中有一个这样的结构文件夹:

函数/index.js

我在 index.js 中添加了一些代码供其他函数使用:

require("dotenv");
const admin = require("firebase-admin");
const config = JSON.parse(process.env.FIREBASE_CONFIG);
admin.initializeApp(config);

const highLevel = {
  timeoutSeconds: 300,
  memory: "2GB",
};

const secretLevel = {
  timeoutSeconds: 120,
  memory: "1GB",
  secret: [process.env.SERVER_BACKEND],
};

const lowLevel = {
  timeoutSeconds: 120,
  memory: "512MB",
};

module.exports = lowLevel, highLevel, secretLevel;

我在这样的文件夹结构中创建了一个文件:

函数/admin/features/music/publisher.js

当我尝试使用这个时,如何从 publisher.js 访问lowLevel的变量:

const lowLevel = require("../index");

而且我无法获得对 index.js 的任何参考。 有没有办法从根文件夹访问文件 index.js? 非常感谢您提供任何提示和技巧。

从这个文件:

 functions/admin/features/music/publisher.js

如果你想访问这个文件:

 functions/index.js

你会这样做:

 const lowLevel = require("../../../index.js");

第一个..向上移动到functions/admin/features目录。

第二个..向上移动到functions/admin目录。

第三个..向上移动到您所说的index.js所在的functions目录。

暂无
暂无

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

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