OriFl
OriFl

Reputation: 127

how to get the absolute path of a file in the same the same directory as the parent folder, from the child file in nodeJS

here is what I mean, my file structure looks like this:

main folder

app.js
child folder

fille.js

I want to get the absolute path of app.js from file.js

Upvotes: 1

Views: 3329

Answers (3)

Yoni Rabinovitch
Yoni Rabinovitch

Reputation: 5261

How about:

let path = __dirname.substr(0,__dirname.lastIndexOf('/'));

Upvotes: 0

Harshal Yeole
Harshal Yeole

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

Sirko
Sirko

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

Related Questions