Get parent directory name in Node.js

node.jsPath

node.js Problem Overview


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.

node.js Solutions


Solution 1 - node.js

What you want is path.basename:

path.basename(path.dirname(filename))

Solution 2 - node.js

Better use @danielwolf's answer instead


Use split() and pop():

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

Solution 3 - node.js

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

require('path').resolve(__dirname, '..')

Solution 4 - node.js

const path = require("path")
path.dirname(path.basename(__dirname))

Solution 5 - node.js

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

Solution 6 - node.js

const path = require('path');

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

Use this anywhere to get the root directory

Solution 7 - node.js

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.

Solution 8 - node.js

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.

Solution 9 - node.js

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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMe5View Question on Stackoverflow
Solution 1 - node.jsDaniel WolfView Answer on Stackoverflow
Solution 2 - node.jsbaaoView Answer on Stackoverflow
Solution 3 - node.jsDirigibleView Answer on Stackoverflow
Solution 4 - node.jsHarsh MangalamView Answer on Stackoverflow
Solution 5 - node.jsByakuView Answer on Stackoverflow
Solution 6 - node.jsC WilliamsView Answer on Stackoverflow
Solution 7 - node.jsDevOpsIsTheNameOfTheGameView Answer on Stackoverflow
Solution 8 - node.jsdanday74View Answer on Stackoverflow
Solution 9 - node.jsVinodh RamView Answer on Stackoverflow