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?
We can get the path of the present script in node. js by using __dirname and __filename module scope variables. __dirname: It returns the directory name of the current module in which the current script is located. __filename: It returns the file name of the current module.
js: var fs = require('fs') var newPath = "E:\\Thevan"; var oldPath = "E:\\Thevan\\Docs\\something. mp4"; exports. uploadFile = function (req, res) { fs. readFile(oldPath, function(err, data) { fs.
__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. Difference between process.cwd() vs __dirname in Node.js is as follows: process.cwd()
In Node. js, there are two built-in ways to get the current directory. You can either use the __dirname variable or the process. cwd() method to get the current folder.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With