简体   繁体   中英

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. I have a structure folders like this in functions directory:

functions/index.js

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

How can I access the variable of lowLevel from publisher.js as I tried using this:

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

and I can't get any of reference to index.js. Is there a way to access file index.js from the root folder? 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.

The second .. moves up to the functions/admin directory.

The third .. moves up to the functions directory where you say that index.js is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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