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
-
Increase the Memory Limit for Your Process -
Why You Should Add “node” in Your Travis Config -
Create a PDF from HTML with Puppeteer and Handlebars -
Create Your Own Custom Error -
Retrieve a Request’s IP Address in Node.js -
Detect the Node.js Version in a Running Process or App -
How to Base64 Encode/Decode a Value in Node.js -
Check if a Value Is Null or Undefined in JavaScript or Node.js -
How to Fix “Uncaught SyntaxError: Cannot use import
statement outside a module”
- Increase the Memory Limit for Your Process
- Why You Should Add “node” in Your Travis Config
-
Create a PDF from HTML with Puppeteer and Handlebars
- Create Your Own Custom Error
- Retrieve a Request’s IP Address in Node.js
- Detect the Node.js Version in a Running Process or App
- How to Base64 Encode/Decode a Value in Node.js
- Check if a Value Is Null or Undefined in
JavaScript or Node.js
- How to Fix “Uncaught SyntaxError: Cannot use import statement outside a module”
-
String Replace All
Appearances -
Remove All Whitespace From a String in JavaScript -
Generate a Random ID or String in Node.js or JavaScript -
Remove Extra Spaces From a String in JavaScript or Node.js -
Remove Numbers From a String in JavaScript or Node.js -
Get the Part Before a Character in a String in JavaScript or Node.js -
Get the Part After a Character in a String in JavaScript or
Node.js -
How to Check if a Value is a String in JavaScript or Node.js -
Check If a String Includes All Strings in
JavaScript/Node.js/TypeScript -
Check if a Value is a String in JavaScript and Node.js -
Limit and Truncate a String to a
Given Length in JavaScript and Node.js -
Split a String into a List of Characters in JavaScript and Node.js -
How to Generage a UUID in Node.js
-
Reverse a String in JavaScript or Node.js -
Split a String into a List of Lines in JavaScript or Node.js -
Split a String into a List of Words in JavaScript or Node.js -
Detect if a String is in camelCase Format in Javascript or Node.js
-
Check If a String Is in Lowercase in JavaScript or Node.js -
Check If a String is in Uppercase in JavaScript or Node.js
-
Get the Part After First Occurrence in a String in JavaScript or Node.js -
Get the Part Before First Occurrence
in a String in JavaScript or Node.js -
Get the Part Before Last Occurrence in a String in JavaScript or Node.js -
Get the Part After Last Occurrence in a String in JavaScript or Node.js
- String Replace All Appearances
- Remove All Whitespace From a String in JavaScript
-
Generate a Random ID or String in Node.js or JavaScript
- Remove Extra Spaces From a String in JavaScript or Node.js
- Remove Numbers
From a String in JavaScript or Node.js
- Get the Part Before a Character in a String in JavaScript or Node.js
- Get the Part After a Character in a String in JavaScript or Node.js
-
How to Check if a Value is a String in JavaScript or Node.js
- Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
-
Check if a Value is a String in JavaScript and Node.js
- Limit and Truncate a String to a Given Length in JavaScript and Node.js
-
Split a String into a List of Characters in JavaScript and Node.js
- How to Generage a UUID in Node.js
- Reverse a String in JavaScript or Node.js
- Split a String into a List of Lines in JavaScript or Node.js
- Split a String into a List of Words in JavaScript or Node.js
-
Detect if a String is in camelCase Format in Javascript or Node.js
- Check If a String Is in Lowercase in JavaScript or Node.js
-
Check If a String is in Uppercase in JavaScript or Node.js
- Get the Part After First Occurrence in a String in JavaScript or Node.js
-
Get the Part Before First Occurrence in a String in JavaScript or Node.js
- Get the Part Before Last Occurrence in a String in JavaScript or Node.js
-
Get the Part After Last Occurrence in a String in JavaScript or Node.js
- Filter Data in Streams
-
Get Number of Seconds Since Epoch in JavaScript -
Get Tomorrow’s Date in JavaScript -
Increase a Date in JavaScript by One Week -
Add Seconds to a Date in Node.js and JavaScript -
Add Month(s) to a Date in JavaScript or Node.js -
Add Week(s) to a Date in JavaScript or Node.js -
Get the Current Year in JavaScript or Node.js -
How to Get a UNIX Timestamp in JavaScript or Node.js -
How to Convert a UNIX Timestamp to a Date in JavaScript or Node.js -
Add Days to a Date in JavaScript or Node.js -
Get Yesterday's Date in JavaScript or Node.js
- Get Number of Seconds Since Epoch in JavaScript
-
Get Tomorrow’s Date in JavaScript
- Increase a Date in JavaScript by One Week
- Add Seconds to a Date in Node.js and JavaScript
- Add Month(s) to a
Date in JavaScript or Node.js
- Add Week(s) to a Date in JavaScript or Node.js
- Get the Current Year in JavaScript or Node.js
- How to Get a
UNIX Timestamp in JavaScript or Node.js
- How to Convert a UNIX Timestamp to a Date in JavaScript or Node.js
- Add Days to a Date in JavaScript or Node.js
-
Get Yesterday's Date in JavaScript or Node.js
-
How to Run an Asynchronous Function in Array.map() -
How to Reset and Empty an Array -
-
Clone/Copy an Array in JavaScript and Node.js
-
Get an Array With Unique Values (Delete Duplicates) -
Sort an Array of Integers in JavaScript and Node.js -
Sort a Boolean Array in JavaScript, TypeScript, or Node.js -
Check If an Array Contains a Given Value in JavaScript or Node.js
-
Add an Item to the Beginning of an Array in JavaScript or Node.js -
Append an Item at the End of an Array in JavaScript or Node.js
-
How to Exit and Stop a for Loop in JavaScript and Node.js -
Split an Array Into Smaller Array Chunks in JavaScript and
Node.js -
How to Get an Index in a for…of Loop in JavaScript and Node.js -
How to Exit, Stop, or Break an
Array#forEach Loop in JavaScript or Node.js -
Retrieve a Random Item From an Array in JavaScript or Node.js -
How to Reverse an Array in
JavaScript and Node.js -
Sort an Array of Strings in JavaScript, TypeScript or Node.js -
Sort an Array of Objects in
JavaScript, TypeScript or Node.js
- How to Run an Asynchronous Function in Array.map()
- How to Reset and Empty an Array
-
for…of vs. for…in Loops
- Clone/Copy an Array in JavaScript and Node.js
- Get an Array With Unique Values (Delete Duplicates)
-
Sort an Array of Integers in JavaScript and Node.js
- Sort a Boolean Array in JavaScript, TypeScript, or Node.js
- Check If
an Array Contains a Given Value in JavaScript or Node.js
- Add an Item to the Beginning of an Array in JavaScript or Node.js
- Append an Item at the End of an Array in JavaScript or Node.js
-
How to Exit and Stop a for Loop in JavaScript and Node.js
- Split an Array Into Smaller Array Chunks in JavaScript and Node.js
-
How to Get an Index in a for…of Loop in JavaScript and Node.js
- How to Exit, Stop, or Break an Array#forEach Loop in JavaScript or Node.js
-
Retrieve a Random Item From an Array in JavaScript or Node.js
- How to Reverse an Array in JavaScript and Node.js
- Sort an
Array of Strings in JavaScript, TypeScript or Node.js
- Sort an Array of Objects in JavaScript, TypeScript or Node.js
-
Callback and Promise
Support in your Node.js Modules -
Run Async Functions/Promises in Sequence -
Run Async Functions/Promises in Parallel -
Run Async Functions in Batches -
How to Fix “Promise resolver undefined is not a function” in Node.js or JavaScript -
Detect if Value Is a Promise in Node.js and JavaScript -
Overview of Promise-Based APIs in Node.js
-
Callback and Promise Support in your Node.js Modules
- Run Async Functions/Promises in Sequence
- Run Async
Functions/Promises in Parallel
- Run Async Functions in Batches
- How to Fix “Promise resolver undefined is not a function” in Node.js or JavaScript
-
Detect if Value Is a Promise in Node.js and JavaScript
- Overview of Promise-Based APIs in Node.js
-
Human-Readable JSON.stringify() With Spaces and Line Breaks -
Write a JSON Object to a File -
Create a Custom “toJSON” Function in Node.js and JavaScript -
-
Human-Readable JSON.stringify() With Spaces and Line Breaks
- Write a JSON Object to a File
- Create a Custom “toJSON” Function in Node.js and JavaScript
-
Securely Parse JSON
-
Check If a Value Is Iterable in JavaScript or Node.js
- Check If a Value Is Iterable in JavaScript
or Node.js
-
Extend Multiple Classes (Multi Inheritance) -
Retrieve the Class Name at Runtime
in JavaScript and Node.js
- Extend Multiple Classes (Multi Inheritance)
- Retrieve the Class Name at Runtime in JavaScript and Node.js
-
Generate a Random Number in Range With JavaScript/Node.js -
Ensure a Positive Number in JavaScript or Node.js -
Check if a Number Is Infinity (Coming soon)
- Generate a Random Number in Range With JavaScript/Node.js
- Ensure a Positive Number in JavaScript or Node.js
- Check if a Number Is Infinity (Coming soon)
-
-
How to Check if an Object is Empty in JavaScript or Node.js -
How to CamelCase Keys of an Object in JavaScript or Node.js -
How to Snake_Case Keys of an Object in JavaScript or Node.js -
How to Destructure a Dynamic Key in JavaScript or Node.js -
How to Get All Keys (Including Symbols) from an Object in JavaScript or
Node.js -
How to Delete a Key From an Object in JavaScript or Node.js -
Iterate Through an Object’s Keys and Values in
JavaScript or Node.js -
How to Convert URLSearchParams to Object
- How to Merge Objects
-
How to Check if an Object is Empty in JavaScript or Node.js
- How to CamelCase Keys of an Object in JavaScript or Node.js
-
How to Snake_Case Keys of an Object in JavaScript or Node.js
- How to Destructure a Dynamic Key in JavaScript or Node.js
-
How to Get All Keys (Including Symbols) from an Object in JavaScript or Node.js
- How to Delete a Key From an Object in JavaScript or Node.js
-
Iterate Through an Object’s Keys and Values in JavaScript or Node.js
- How to Convert URLSearchParams to Object
-
Get a File’s Created Date -
Get a File’s Last Modified or Updated Date of a File -
How to Create an Empty File -
Check If a Path or File Exists -
-
Check If a Path Is a Directory -
Check If a Path Is a File -
Retrieve the Path to the User’s Home Directory -
-
Read File Content as String -
Check If a Directory Is Empty -
How to Create a Directory (and Parents If Needed) -
-
-
Get a File Name (With or Without Extension) -
-
Get a File’s Created Date
- Get a File’s Last Modified or Updated Date of a File
- How to Create an Empty File
-
Check If a Path or File Exists
- How to Rename a File
- Check If a Path Is a Directory
- Check
If a Path Is a File
- Retrieve the Path to the User’s Home Directory
- How to Touch a File
- Read File Content as String
-
Check If a Directory Is Empty
- How to Create a Directory (and Parents If Needed)
- Get a File‘s Extension
-
Get the Size of a File
- Get a File Name (With or Without Extension)
- Read a JSON File
- Create From Object
-
Transform to an Object
-
Determine the Node.js Version Running Your Script
-
Determine the Node.js Version Running Your Script
-
Check if a Value is a Symbol in JavaScript or Node.js
-
Check if a Value is a Symbol in JavaScript or Node.js
-
Detect if Running on Linux -
Detect if Running on macOS -
Detect if Running on Windows -
Check if Running on 64bit or 32bit Platform
- Detect if Running on Linux
- Detect if Running on macOS
-
Detect if Running on Windows
- Check if Running on 64bit or 32bit Platform
- How to Download a File
-
(Coming soon) -
Retrieve the List of Supported Hash Algorithms (Coming soon) -
(Coming soon)
- Calculate an MD5 Hash (Coming soon)
- Retrieve the List of Supported Hash Algorithms (Coming soon)
- Calculate a SHA256 Hash (Coming soon)
Get the File Name With ExtensionYou
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 ExtensionSometimes 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 PackageI’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 extensionFs.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öhlsMarcus 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.
|