To use the ensureDirSync()
function from the fs-extra
package in Node.js, you need to follow the below steps:
Install fs-extra
package using npm, using the command:
index.tsx21 chars2 lines
Import the fs-extra
package in your JavaScript file:
index.tsx32 chars2 lines
Call the ensureDirSync()
function and pass the directory path as a parameter to create the directory if it does not exist.
index.tsx69 chars3 lines
If the directory already exists, this function will not modify it in any way.
You can also pass an options object with some properties to modify the behavior of the function.
index.tsx116 chars6 lines
In the above example, 0o2775 represents the mode of the directory (in octal notation). The ensureDirSync()
function will create the directory with the mode specified in the options
object.
That's it! The ensureDirSync()
function will create the specified directory if it does not exist and return silently if it already exists.
gistlibby LogSnag