Home > front end >  In Node.js, How can I access exported module in Index.js?
In Node.js, How can I access exported module in Index.js?

Time:03-09

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.

CodePudding user response:

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.

  • Related