Reputation: 127
here is what I mean, my file structure looks like this:
main folder
app.js
child folderfille.js
I want to get the absolute path of app.js from file.js
Upvotes: 1
Views: 3329
Reputation: 5261
How about:
let path = __dirname.substr(0,__dirname.lastIndexOf('/'));
Upvotes: 0
Reputation: 4983
It's easy:
You can try path.resolve
try:
resolve = require('path').resolve
resolve('../../app.js')
You will need to provide the relative path and it will give you absolute path. Hope this is what you are looking for.
Upvotes: 1
Reputation: 74046
So assuming you are inside file.js
and want to get the absolute path of app.js
, with child folder
being fixed, you can use path.join()
:
const path = require( 'path' ).join( __dirname, '..', 'app.js' )
Upvotes: 2