1

I want to acces files outside my main.js folder,below is my folder structure:

ent
 src
   ep
    main.js
 config
   abc.js
   xyz.js

I have tried doing var config_src = __dirname+"/../../config" it works fine, but not acceptable bt my TL.What is the other way to get the path of congig folder?

1
  • Can you explain what a TL is? Commented Feb 16, 2018 at 13:06

2 Answers 2

5

__dirname returns your working (project) directory. then you have out from that folder. suppose your folder structure and using sonfig_src in app.js :

somefoledr
   |___somefolder1
   |___somefolder2
          |__config
yourProject
   |___ app.js

One more thing you did not used const path = require('path') package.

Now your code should look like this :

const path = require('path')
const config_src = path.resolve(__dirname, "/../somefoledr/somefoledr2/config")

But it can be harmful when you will deploy it on server, it could create path problem !

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

5 Comments

I want to completely remeove the usage of "../" and fetch the absolute path of config folder
I tried a lot but using varoius ways, bt could get path till the src folder, how can i fetch the path for config without usage of "../"
checkout this link, m sure you will find you solution here : nodejs.org/api/path.html
but setting path this way will not work when the project is deployed. The path may not be the sane
I tried using require('app-root-path') which gives me the path till src directory, but again am stuck how to navigate till config.I dont want to use "../" for that purpose
1

Use path.resolve

const path = require('path')
const config_src = path.resolve(__dirname, "../../config")

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.