node js get file name without extension
Getting the File Name Without Extension in Node.js
To get the file name without the extension in Node.js, you can use the path
module and its basename
function. Here are the steps to achieve this:
Import the
path
module by requiring it at the beginning of your code:javascript const path = require('path');
Use the
path.basename
function to get the file name without the extension. Pass the file path as the first argument andpath.extname
as the second argument to remove the extension:javascript const filePath = '/path/to/file.txt'; const fileName = path.basename(filePath, path.extname(filePath));
In this example, the filePath
variable contains the path to the file, and fileName
will store the file name without the extension.
Note: Make sure to replace /path/to/file.txt
with the actual file path you want to extract the name from.
- The
fileName
variable will now hold the file name without the extension. You can use it as needed in your code.
Here's the complete code snippet:
const path = require('path');
const filePath = '/path/to/file.txt';
const fileName = path.basename(filePath, path.extname(filePath));
console.log(fileName);
This code will output the file name without the extension to the console.
Let me know if you need further assistance!