Skip to content

All topics  /  Node.js

Node JS Get Filename Without Extension

Node.js

Sep 21, 2022

Hello Friends,

Today, node js get filename without extension is our main topic. I explained simply step by step get filename without extension in node js. We will look at an example of get a filename without extension in nodejs. you can see get filename without extension in node.js.

I will explain this example node js get filename without extension. You may use the built-in node.js path module to do that. You can use the Path.parse method to parse a given file path into the individual parts.

So, let's start following example with output:

Step 1: Install Node JS


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app
cd my-app

 
npm init

Step 2: Update server.js file

server.js

// import package
const path = require('path');
// get filename without extension
const fileName = path.parse('index.html').name
// print file name without extension
console.log(fileName);

Output:

index