0

In my code, I need to call a sub-module, and in the sub-module I need to require a file that only has a relative path. So I need to join the 'current main' code path to the relative path.

I tried to use:

var conf = require(path.resolve(__dirname + ConfigPath));

But __dirname will get me the current sub-module path. What I need here is the main code path.

Can anyone please help here? many thanks!

1 Answer 1

1

(Node.JS context only)

For your "sub-module" (the generic approach): use __dirname

For your "parent-module" (or even the topmost / main module): use process.mainModule See further details @ https://nodejs.org/api/process.html#process_process_mainmodule

This is how you can further processs the mainModule information.

var fnArr = (process.mainModule.filename).split('/'); var mmFilename = fnArr[fnArr.length -1];

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.