Node js Get folder path from a file

blessenm picture blessenm · Jun 19, 2013 · Viewed 74.2k times · Source

Is there a way to get the path to a folder that holds a particular file.

fs.realpathSync('config.json', []);

returns something like

G:\node-demos\7-node-module\demo\config.json

I just need

G:\node-demos\7-node-module\demo\ 
or
G:\node-demos\7-node-module\demo\

Is there any api for this or will I need to process the string?

Answer

hereandnow78 picture hereandnow78 · Jun 19, 2013

use path.dirname

// onlyPath should be G:\node-demos\7-handlebars-watch\demo
var onlyPath = require('path').dirname('G:\\node-demos\\7-node-module\\demo\\config.json');