Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get parent directory name in Node.js

Tags:

path

node.js

I am using Node.js, and I want to obtain the parent directory name for a file. I have the file "../test1/folder1/FolderIWant/test.txt".

I want to get "FolderIWant".

I have tried:

var path = require('path');
var parentDir = path.dirname(filename);

But it returns ../test1/folder1/FolderIWant.

like image 709
Me5 Avatar asked Mar 22 '17 15:03

Me5


People also ask

How to get parent directory in Node js?

To get the parent directory name in Node. js, we can use the path. basename and path. dirname methods.

How to get the directory name in Node js?

console. log("Current directory:", __dirname); The variable will print the path to the directory, starting from the root directory. And just like that, you can get the current working directory in NodeJS.

How do I get the parent directory of a file?

Use File 's getParentFile() method and String. lastIndexOf() to retrieve just the immediate parent directory.

What is __ Dirname in node?

__dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.


9 Answers

What you want is path.basename:

path.basename(path.dirname(filename))
like image 151
Daniel Wolf Avatar answered Oct 05 '22 13:10

Daniel Wolf


Better use @danielwolf's answer instead


Use split() and pop():

path.dirname(filename).split(path.sep).pop()
like image 28
baao Avatar answered Oct 05 '22 15:10

baao


Daniel Wolf's answer is correct, also if you want the full path of the parent dir:

require('path').resolve(__dirname, '..')
like image 21
Dirigible Avatar answered Oct 05 '22 14:10

Dirigible


const path = require("path")
path.dirname(path.basename(__dirname))
like image 35
Harsh Mangalam Avatar answered Oct 05 '22 14:10

Harsh Mangalam


process.mainModule property is deprecated in v14.0.0. If foo.js is run by node foo.js (e.g. somedir/foo.js"),

const path = require("path");

module.exports = path.dirname(require.main.filename);

result: somedir

Use require.main instead

like image 23
Byaku Avatar answered Oct 05 '22 14:10

Byaku


Whilst the answers herein worked somewhat, I found use of the popular app-root-path module a better anchor point from which to specify a path.

import { path as arp } from 'app-root-path'
import path from 'path'

const root = path.resolve(arp, '../') // the parent of the root path

export const rootDirname = root

Example usage as follows:

import { rootDirname } from './functions/src/utils/root-dirname'
import { getJsonFromFile } from './app/utils/get-json-from-file'

const firebaseJson = getJsonFromFile(`${rootDirname}/firebase.json`)

Maybe not the best answer here but an option not covered by other answers.

like image 29
danday74 Avatar answered Oct 05 '22 15:10

danday74


const path = require('path');

module.exports = path.dirname(process.mainModule.filename)

Use this anywhere to get the root directory

like image 33
C Williams Avatar answered Oct 05 '22 14:10

C Williams


Using node as of 06-2019, I ran into an issue for accessing just filename. So instead, I just modified it a tiny bit and used:

path.dirname(__filename).split(path.sep).pop()

so now you get the directory name of the current directory you are in and not the full path. Although the previous answers seem to possibly work for others, for me it caused issues as node was looking for a const or a variable but couldn't find one.

like image 24
DevOpsIsTheNameOfTheGame Avatar answered Oct 05 '22 14:10

DevOpsIsTheNameOfTheGame


Simplest way without any node modules like the path. You can easily do in the following manner to get the root folder name.

var rootFolder = __dirname.split('/').pop();
console.log(rootFolder);
like image 42
Vinodh Ram Avatar answered Oct 05 '22 14:10

Vinodh Ram