banner



How To Create Folder In Node Js

This article walks you through a couple of different ways to create a directory if it doesn't exist in Node.js.

Using the FS module

FS which stands for "file system", is a standard module of Node.js so you can use it without installing any third-party packages. The module provides some useful methods: Advertisements

  • existsSync: Used to synchronously check whether a file or a directory exists.
  • mkdirSync: Used to synchronously create a new directory with a given name.
  • exists: Used to asynchronously test if a given path exists. This one is depreciated and you should no longer call it in new projects.
  • mkdir: Used to asynchronously create a new directory.

Example 1

          const fs = require('fs')  const directory = './some-dir' if(!fs.existsSync(directory)){   fs.mkdirSync(directory) }        

And here's the project structure after that: Advertisements

          . ├── Readme.md ├── package-lock.json ├── package.json ├── some-dir └── src     └── index.js        

Example 2

          const fs = require('fs')  const dir = './new-folder'  fs.mkdir(dir, (err) => {   if(err){     console.log('The directory already exists!')   } else {     console.log('Successfully created a new directory')   } })        

In the examples above, the path of the directory is the related path to your package.json file.

Using a third-party package

What if you want to create a directory that nests deep in many other directories that even don't exist? For instance, the path is:

          const dir = './some/deep/path/kindacode/node-tutorials'        

The solutions are you can perform multiple steps iteratively using the above method or using a third-party library that can help us get the job done with only 1 or 2 lines of code. There are so many names to chose from, such as make-dir, makdirp, fs-extra.

In the example below, we'll use fs-extra. You can add it to your project by running:

          npm i fs-extra        

Using the ensureDir method provided by the library to ensure that a given directory exists. If the directory structure does not exist, it will be created:

          const fse = require('fs-extra')  const dir = './some/deep/path/kindacode/node-tutorials' fse.ensureDir(dir);        

Conclusion

Advertisements

We've gone through a few ways of creating a new directory if it doesn't exist with Node.js. If you are a backend developer then the occasions that you need them are not rare.

Node.js is awesome and many people love it. If you'd like to learn more new and interesting things about this Javascript runtime, take a look at the following articles:

  • 4 Ways to Convert Object into Query String in Node.js
  • Node.js: How to Use "Import" and "Require" in the Same File
  • 6 best Node.js frameworks to build backend APIs
  • Top 4 best Node.js Open Source Headless CMS
  • Using Axios to download images and videos in Node.js

You can also check out our Node.js category page or PHP category page for the latest tutorials and examples.

How To Create Folder In Node Js

Source: https://www.kindacode.com/article/node-js-ways-to-create-a-directory-if-it-doesnt-exist/

Posted by: taylorcultin.blogspot.com

0 Response to "How To Create Folder In Node Js"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel