Hướng dẫn nodejs __dirname parent

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.

Hướng dẫn nodejs __dirname parent

asked Mar 22, 2017 at 15:29

What you want is path.basename:

path.basename(path.dirname(filename))

answered May 4, 2017 at 9:57

Daniel WolfDaniel Wolf

11.9k11 gold badges51 silver badges74 bronze badges

0

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

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

answered Mar 14, 2019 at 15:52

Hướng dẫn nodejs __dirname parent

DirigibleDirigible

1,54915 silver badges10 bronze badges

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

Yeshi

2132 silver badges5 bronze badges

answered Jul 20, 2020 at 3:03

Hướng dẫn nodejs __dirname parent

1

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

answered Jul 5, 2020 at 6:23

ByakuByaku

2,0191 gold badge16 silver badges27 bronze badges

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.

answered Oct 20, 2021 at 1:53

Hướng dẫn nodejs __dirname parent

danday74danday74

47.8k45 gold badges209 silver badges259 bronze badges

const path = require('path');

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

Use this anywhere to get the root directory

Hướng dẫn nodejs __dirname parent

answered Jun 4, 2019 at 18:43

C WilliamsC Williams

82211 silver badges18 bronze badges

2

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.

answered Jun 19, 2019 at 8:45

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

answered Aug 8, 2020 at 11:41

Hướng dẫn nodejs __dirname parent

Vinodh RamVinodh Ram

6491 gold badge9 silver badges21 bronze badges

The typical __dirname solution doesn't work in an ESM scope. To move one level higher in the directory tree, I came up with the following solution:

import { fileURLToPath } from "url";
import path from "path";

const __filename = fileURLToPath(import.meta.url);
// First find out the __dirname, then resolve to one higher level in the dir tree
const __dirname = path.resolve(path.dirname(__filename), "../");

If you only need the absolute directory path the file resides in, you can leave out the path.resolve() call.

answered Aug 1 at 10:40

Hướng dẫn nodejs __dirname parent

Tom BöttgerTom Böttger

4337 silver badges10 bronze badges