简体   繁体   English

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

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

I have searched in stackoverflow and it seems there isn't any use case like mine.我在 stackoverflow 中搜索过,似乎没有像我这样的用例。 I have a structure folders like this in functions directory:我在函数目录中有一个这样的结构文件夹:

functions/index.js函数/index.js

I added some codes to be used by other functions like this in 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;

I created a file in a folder structure like this:我在这样的文件夹结构中创建了一个文件:

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

How can I access the variable of lowLevel from publisher.js as I tried using this:当我尝试使用这个时,如何从 publisher.js 访问lowLevel的变量:

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

and I can't get any of reference to index.js.而且我无法获得对 index.js 的任何参考。 Is there a way to access file index.js from the root folder?有没有办法从根文件夹访问文件 index.js? Thank you very much for any tips and trick.非常感谢您提供任何提示和技巧。

From this file:从这个文件:

 functions/admin/features/music/publisher.js

If you want access to this file:如果你想访问这个文件:

 functions/index.js

You would do that with:你会这样做:

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

The first .. moves up to the functions/admin/features directory.第一个..向上移动到functions/admin/features目录。

The second .. moves up to the functions/admin directory.第二个..向上移动到functions/admin目录。

The third .. moves up to the functions directory where you say that index.js is.第三个..向上移动到您所说的index.js所在的functions目录。

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

相关问题 如何让 $node index.js 在我的命令行中运行 index.js? - How can I get $node index.js to run index.js in my command line? 为什么我不能在index.js中使用导出的路由 - Why I can not use exported routes in index.js 如何在用 Node.js 编写的 index.js 中调用 function - How to invoke a function in index.js written in Node.js 我如何在Ubuntu中运行“ node index.js” - How can i run “node index.js” in ubuntu node.js命令行程序(使用Commander节点模块)在执行时打开index.js - node.js command line programs(using commander node-module) opens index.js on execution 使用index.js作为条目运行node.js服务器时,Module.compile出现SyntaxError“ c” - SyntaxError “��c” at Module.compile when running node.js server using `index.js` as entry 错误:文件 index.js 定义的 Node.js 模块应该导出名为 xxxx 的函数 - Error: Node.js module defined by file index.js is expected to export function named xxxx Node.js 只解析 dist/index.js 模块中其他文件未找到 - Node.js only resolving dist/index.js other files in module are not found 如何在Node.js模块内部引用导出的函数 - How to reference an exported function inside of a Node.js module 如何从node.js中的导出模块调用函数? - How to call a function from a exported module in node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM