Get parent directory name in Node.js

前端 未结 8 1522
北海茫月
北海茫月 2021-01-31 13:27

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

8条回答
  •  逝去的感伤
    2021-01-31 13:58

    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.

提交回复
热议问题