Nodejs path get filename without extension

Moonshoot

  • Intro
  • Get the File Name With Extension
  • Get the File Name Without Extension
  • Use the @supercharge/fs Package

Node.js can extract a file name from a given path. The path can be an absolute or relative path. You can then let Node.js do the parsing and return the file path.

Node.js Series Overview

  • Node.js
  • Strings
  • Streams
  • Date & Time
  • Arrays
  • Promises
  • JSON
  • Iterators
  • Classes
  • Numbers
  • Objects
  • File System
  • Map
  • Process
  • Symbols
  • Platform/OS
  • HTTPS
  • Hashing

  1. Increase the Memory Limit for Your Process

  2. Why You Should Add “node” in Your Travis Config

  3. Create a PDF from HTML with Puppeteer and Handlebars

  4. Create Your Own Custom Error

  5. Retrieve a Request’s IP Address in Node.js

  6. Detect the Node.js Version in a Running Process or App

  7. How to Base64 Encode/Decode a Value in Node.js

  8. Check if a Value Is Null or Undefined in JavaScript or Node.js

  9. How to Fix “Uncaught SyntaxError: Cannot use import statement outside a module”

  1. Increase the Memory Limit for Your Process
  2. Why You Should Add “node” in Your Travis Config
  3. Create a PDF from HTML with Puppeteer and Handlebars
  4. Create Your Own Custom Error
  5. Retrieve a Request’s IP Address in Node.js
  6. Detect the Node.js Version in a Running Process or App
  7. How to Base64 Encode/Decode a Value in Node.js
  8. Check if a Value Is Null or Undefined in JavaScript or Node.js
  9. How to Fix “Uncaught SyntaxError: Cannot use import statement outside a module”

  1. String Replace All Appearances

  2. Remove All Whitespace From a String in JavaScript

  3. Generate a Random ID or String in Node.js or JavaScript

  4. Remove Extra Spaces From a String in JavaScript or Node.js

  5. Remove Numbers From a String in JavaScript or Node.js

  6. Get the Part Before a Character in a String in JavaScript or Node.js

  7. Get the Part After a Character in a String in JavaScript or Node.js

  8. How to Check if a Value is a String in JavaScript or Node.js

  9. Check If a String Includes All Strings in JavaScript/Node.js/TypeScript

  10. Check if a Value is a String in JavaScript and Node.js

  11. Limit and Truncate a String to a Given Length in JavaScript and Node.js

  12. Split a String into a List of Characters in JavaScript and Node.js

  13. How to Generage a UUID in Node.js

  14. Reverse a String in JavaScript or Node.js

  15. Split a String into a List of Lines in JavaScript or Node.js

  16. Split a String into a List of Words in JavaScript or Node.js

  17. Detect if a String is in camelCase Format in Javascript or Node.js

  18. Check If a String Is in Lowercase in JavaScript or Node.js

  19. Check If a String is in Uppercase in JavaScript or Node.js

  20. Get the Part After First Occurrence in a String in JavaScript or Node.js

  21. Get the Part Before First Occurrence in a String in JavaScript or Node.js

  22. Get the Part Before Last Occurrence in a String in JavaScript or Node.js

  23. Get the Part After Last Occurrence in a String in JavaScript or Node.js

  1. String Replace All Appearances
  2. Remove All Whitespace From a String in JavaScript
  3. Generate a Random ID or String in Node.js or JavaScript
  4. Remove Extra Spaces From a String in JavaScript or Node.js
  5. Remove Numbers From a String in JavaScript or Node.js
  6. Get the Part Before a Character in a String in JavaScript or Node.js
  7. Get the Part After a Character in a String in JavaScript or Node.js
  8. How to Check if a Value is a String in JavaScript or Node.js
  9. Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
  10. Check if a Value is a String in JavaScript and Node.js
  11. Limit and Truncate a String to a Given Length in JavaScript and Node.js
  12. Split a String into a List of Characters in JavaScript and Node.js
  13. How to Generage a UUID in Node.js
  14. Reverse a String in JavaScript or Node.js
  15. Split a String into a List of Lines in JavaScript or Node.js
  16. Split a String into a List of Words in JavaScript or Node.js
  17. Detect if a String is in camelCase Format in Javascript or Node.js
  18. Check If a String Is in Lowercase in JavaScript or Node.js
  19. Check If a String is in Uppercase in JavaScript or Node.js
  20. Get the Part After First Occurrence in a String in JavaScript or Node.js
  21. Get the Part Before First Occurrence in a String in JavaScript or Node.js
  22. Get the Part Before Last Occurrence in a String in JavaScript or Node.js
  23. Get the Part After Last Occurrence in a String in JavaScript or Node.js

  1. Filter Data in Streams

  1. Get Number of Seconds Since Epoch in JavaScript

  2. Get Tomorrow’s Date in JavaScript

  3. Increase a Date in JavaScript by One Week

  4. Add Seconds to a Date in Node.js and JavaScript

  5. Add Month(s) to a Date in JavaScript or Node.js

  6. Add Week(s) to a Date in JavaScript or Node.js

  7. Get the Current Year in JavaScript or Node.js

  8. How to Get a UNIX Timestamp in JavaScript or Node.js

  9. How to Convert a UNIX Timestamp to a Date in JavaScript or Node.js

  10. Add Days to a Date in JavaScript or Node.js

  11. Get Yesterday's Date in JavaScript or Node.js

  1. Get Number of Seconds Since Epoch in JavaScript
  2. Get Tomorrow’s Date in JavaScript
  3. Increase a Date in JavaScript by One Week
  4. Add Seconds to a Date in Node.js and JavaScript
  5. Add Month(s) to a Date in JavaScript or Node.js
  6. Add Week(s) to a Date in JavaScript or Node.js
  7. Get the Current Year in JavaScript or Node.js
  8. How to Get a UNIX Timestamp in JavaScript or Node.js
  9. How to Convert a UNIX Timestamp to a Date in JavaScript or Node.js
  10. Add Days to a Date in JavaScript or Node.js
  11. Get Yesterday's Date in JavaScript or Node.js

  1. How to Run an Asynchronous Function in Array.map()

  2. How to Reset and Empty an Array

  3. Clone/Copy an Array in JavaScript and Node.js

  4. Get an Array With Unique Values (Delete Duplicates)

  5. Sort an Array of Integers in JavaScript and Node.js

  6. Sort a Boolean Array in JavaScript, TypeScript, or Node.js

  7. Check If an Array Contains a Given Value in JavaScript or Node.js

  8. Add an Item to the Beginning of an Array in JavaScript or Node.js

  9. Append an Item at the End of an Array in JavaScript or Node.js

  10. How to Exit and Stop a for Loop in JavaScript and Node.js

  11. Split an Array Into Smaller Array Chunks in JavaScript and Node.js

  12. How to Get an Index in a for…of Loop in JavaScript and Node.js

  13. How to Exit, Stop, or Break an Array#forEach Loop in JavaScript or Node.js

  14. Retrieve a Random Item From an Array in JavaScript or Node.js

  15. How to Reverse an Array in JavaScript and Node.js

  16. Sort an Array of Strings in JavaScript, TypeScript or Node.js

  17. Sort an Array of Objects in JavaScript, TypeScript or Node.js

  1. How to Run an Asynchronous Function in Array.map()
  2. How to Reset and Empty an Array
  3. for…of vs. for…in Loops
  4. Clone/Copy an Array in JavaScript and Node.js
  5. Get an Array With Unique Values (Delete Duplicates)
  6. Sort an Array of Integers in JavaScript and Node.js
  7. Sort a Boolean Array in JavaScript, TypeScript, or Node.js
  8. Check If an Array Contains a Given Value in JavaScript or Node.js
  9. Add an Item to the Beginning of an Array in JavaScript or Node.js
  10. Append an Item at the End of an Array in JavaScript or Node.js
  11. How to Exit and Stop a for Loop in JavaScript and Node.js
  12. Split an Array Into Smaller Array Chunks in JavaScript and Node.js
  13. How to Get an Index in a for…of Loop in JavaScript and Node.js
  14. How to Exit, Stop, or Break an Array#forEach Loop in JavaScript or Node.js
  15. Retrieve a Random Item From an Array in JavaScript or Node.js
  16. How to Reverse an Array in JavaScript and Node.js
  17. Sort an Array of Strings in JavaScript, TypeScript or Node.js
  18. Sort an Array of Objects in JavaScript, TypeScript or Node.js

  1. Callback and Promise Support in your Node.js Modules

  2. Run Async Functions/Promises in Sequence

  3. Run Async Functions/Promises in Parallel

  4. Run Async Functions in Batches

  5. How to Fix “Promise resolver undefined is not a function” in Node.js or JavaScript

  6. Detect if Value Is a Promise in Node.js and JavaScript

  7. Overview of Promise-Based APIs in Node.js

  1. Callback and Promise Support in your Node.js Modules
  2. Run Async Functions/Promises in Sequence
  3. Run Async Functions/Promises in Parallel
  4. Run Async Functions in Batches
  5. How to Fix “Promise resolver undefined is not a function” in Node.js or JavaScript
  6. Detect if Value Is a Promise in Node.js and JavaScript
  7. Overview of Promise-Based APIs in Node.js

  1. Human-Readable JSON.stringify() With Spaces and Line Breaks

  2. Write a JSON Object to a File

  3. Create a Custom “toJSON” Function in Node.js and JavaScript

  1. Human-Readable JSON.stringify() With Spaces and Line Breaks
  2. Write a JSON Object to a File
  3. Create a Custom “toJSON” Function in Node.js and JavaScript
  4. Securely Parse JSON

  1. Check If a Value Is Iterable in JavaScript or Node.js

  1. Check If a Value Is Iterable in JavaScript or Node.js

  1. Extend Multiple Classes (Multi Inheritance)

  2. Retrieve the Class Name at Runtime in JavaScript and Node.js

  1. Extend Multiple Classes (Multi Inheritance)
  2. Retrieve the Class Name at Runtime in JavaScript and Node.js

  1. Generate a Random Number in Range With JavaScript/Node.js

  2. Ensure a Positive Number in JavaScript or Node.js

  3. Check if a Number Is Infinity

    (Coming soon)

  1. Generate a Random Number in Range With JavaScript/Node.js
  2. Ensure a Positive Number in JavaScript or Node.js
  3. Check if a Number Is Infinity (Coming soon)

  1. How to Check if an Object is Empty in JavaScript or Node.js

  2. How to CamelCase Keys of an Object in JavaScript or Node.js

  3. How to Snake_Case Keys of an Object in JavaScript or Node.js

  4. How to Destructure a Dynamic Key in JavaScript or Node.js

  5. How to Get All Keys (Including Symbols) from an Object in JavaScript or Node.js

  6. How to Delete a Key From an Object in JavaScript or Node.js

  7. Iterate Through an Object’s Keys and Values in JavaScript or Node.js

  8. How to Convert URLSearchParams to Object

  1. How to Merge Objects
  2. How to Check if an Object is Empty in JavaScript or Node.js
  3. How to CamelCase Keys of an Object in JavaScript or Node.js
  4. How to Snake_Case Keys of an Object in JavaScript or Node.js
  5. How to Destructure a Dynamic Key in JavaScript or Node.js
  6. How to Get All Keys (Including Symbols) from an Object in JavaScript or Node.js
  7. How to Delete a Key From an Object in JavaScript or Node.js
  8. Iterate Through an Object’s Keys and Values in JavaScript or Node.js
  9. How to Convert URLSearchParams to Object

  1. Get a File’s Created Date

  2. Get a File’s Last Modified or Updated Date of a File

  3. How to Create an Empty File

  4. Check If a Path or File Exists

  5. Check If a Path Is a Directory

  6. Check If a Path Is a File

  7. Retrieve the Path to the User’s Home Directory

  8. Read File Content as String

  9. Check If a Directory Is Empty

  10. How to Create a Directory (and Parents If Needed)

  11. Get a File Name (With or Without Extension)

  1. Get a File’s Created Date
  2. Get a File’s Last Modified or Updated Date of a File
  3. How to Create an Empty File
  4. Check If a Path or File Exists
  5. How to Rename a File
  6. Check If a Path Is a Directory
  7. Check If a Path Is a File
  8. Retrieve the Path to the User’s Home Directory
  9. How to Touch a File
  10. Read File Content as String
  11. Check If a Directory Is Empty
  12. How to Create a Directory (and Parents If Needed)
  13. Get a File‘s Extension
  14. Get the Size of a File
  15. Get a File Name (With or Without Extension)
  16. Read a JSON File

  1. Create From Object
  2. Transform to an Object

  1. Determine the Node.js Version Running Your Script

  1. Determine the Node.js Version Running Your Script

  1. Check if a Value is a Symbol in JavaScript or Node.js

  1. Check if a Value is a Symbol in JavaScript or Node.js

  1. Detect if Running on Linux

  2. Detect if Running on macOS

  3. Detect if Running on Windows

  4. Check if Running on 64bit or 32bit Platform

  1. Detect if Running on Linux
  2. Detect if Running on macOS
  3. Detect if Running on Windows
  4. Check if Running on 64bit or 32bit Platform

  1. How to Download a File

  1. (Coming soon)

  2. Retrieve the List of Supported Hash Algorithms

    (Coming soon)

  3. (Coming soon)

  1. Calculate an MD5 Hash (Coming soon)
  2. Retrieve the List of Supported Hash Algorithms (Coming soon)
  3. Calculate a SHA256 Hash (Coming soon)

Get the File Name With Extension

You can retrieve the file name from a given path using Node.js’ path module. The path module comes with the Node.js runtime.

Retrieve the file name using the Path.basename method. This method returns the trailing name component from a file path, including the file extension. For example, it returns avatar.png from the path /home/user/avatar.png:

import Path from 'path'

const filename = Path.basename('/home/user/avatar.png')  
// 'avatar.png'

Get the File Name Without Extension

Sometimes you want to retrieve the file name without the 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. Then, retrieve the file name without the extension:

import Path from 'path'

const filename = Path.parse('/home/user/avatar.png').name  
// 'avatar'

Use the @supercharge/fs Package

I’m the maintainer of the @supercharge/fs package providing convenient file system utilities. The @supercharge/fs package comes with handy methods to retrieve the file name:

  • Fs.basename(path): returns the file name with extension
  • Fs.basename(path): returns the file name without extension

Here’s the sample code on how to use both functions:

import Fs from '@supercharge/fs'

Fs.basename('/home/user/avatar.png')  
// 'avatar.png'

Fs.filename('/home/user/avatar.png')  
// 'avatar'

That’s it!


Mentioned Resources

  • @supercharge/fs repository on GitHub

Get Notified on New Future Studio Content and Platform Updates

Get your weekly push notification about new and trending
Future Studio content and recent platform enhancements

Marcus Pöhls

Marcus is a fullstack JS developer. He’s passionate about the hapi framework for Node.js and loves to build web apps and APIs. Creator of Futureflix and the “learn hapi” learning path.

How do I find the filename in node JS?

The path. basename() method returns the filename part of a file path.

How do I get the FS filename?

“fs gets file name” Code Answer's.
const fs = require('fs').
const dir = '/Users/flavio/folder'.
const files = fs. readdirSync(dir).
for (const file of files) {.
console. log(file).

What is path Basename?

The path. basename() method is used to get the filename portion of a path to the file. The trailing directory separators are ignored when using this method.

How do I find the base path in node JS?

You can use process. cwd() which returns the current working directory of the process. That command works fine if you execute your node application from the base application directory. However, if you execute your node application from different directory, say, its parent directory (e.g. node yourapp\index.