MOUSE MOUSE - 11 months ago 54
Node.js Question

node.js/javascript - how to map a variable in a relative path

I have a

and I'm using this variable in multiple
files. So the same path is set every time I use this variable.

Is there any way I can use this variable as a global variable and I can hardcode this location only once, such as:

var setFileLocation = '/home/name/parent1/parentdirec/direc/qa.json';

Answer Source

As you are using node you can use require

I do this with config info like this

in one file called config.js for example

module.exports = {
    v1: somevalue,
    fileLocation: "/default/file/loc",
    computePath: (dir, subdir, file) {
         //use Vasans example here

Then in your code require config.js

var config = require('./config.js');

And you will have access to the variable or the funtion to create the path, whichever way you want