Is there a way to verify if a folder exists at the root of the project?

I'm currently working on a NPM script where I have a folder called "scripts" that contains all of my scripts. My goal is to check if there is a folder named "docs" at the root of the project, and if it exists, delete it. If not, then perform another task. I am utilizing fs-extra to interact with directories.

const fs = require('fs-extra');
const directory = 'docs';
try {
    if (!fs.existsSync(directory)) {
        fs.unlinkSync(directory);
        console.log("Unwanted folder removed from the boilerplate.");
    }
} catch (error) {
    console.error(error);
}

I attempted to run this script, but unfortunately, it did not have any effect.

Answer №1

For those utilizing fs-extra, there is the option to utilize the removeSync method:

This method is designed to delete either a file or directory, even if the directory contains other items. If the specified path does not exist, it will simply do nothing.

Answer №2

If you want to utilize the File System through the Node API, here's a simple way to do it.

const fs = require('fs');
fs.stat('YOUR_DIRECTORY_PATH', (error, stats) => {

  // Directory does not exist
  if (error) throw error

  // The given path is not a directory
  if (!stats.isDirectory()) throw "This is not a directory"

  performYourTasks()
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to incorporate multiple plan tiers on Stripe's platform?

//api/stripe import { auth, currentUser } from "@clerk/nextjs/server"; import { NextResponse } from "next/server"; import { prismadb } from "@/lib/prismadb"; import { stripe } from "@/lib/stripe"; import { absoluteUr ...

Retrieve the name of the path for the specified * stack within Express.js

When working with Express.js, I am utilizing '*' to catch the error 404. Is there a way for me to find out the path name of the error URL? app.get('*', (req, res) => { console.log("route: " + JSON.stringify(req.route) ...

Adjust the size of an iFrame's content according to the dimensions of the iFrame

I am looking to use an iFrame for embedding a map, and my goal is to resize the map according to the values in the iFrame tag. Most tutorials I have found focus on auto-sizing the iFrame based on content, which is not what I need. Just to make it clear, I ...

Utilizing web components in React by solely relying on a CDN for integration

I have a client who has provided us with a vast library of UI elements that they want incorporated into our project. These elements are stored in javascript and css files on a CDN, and unfortunately I do not have access to the source code. All I have at my ...

How can you showcase the content of an object within an array?

I am currently working on a project component that involves retrieving and logging data. However, I am facing an issue with accessing objects within my array of clients to display them properly in my table. Here is the script I am using: <script> ex ...

What could be causing AngularJS to truncate my URL in the search bar?

Currently, I am in the process of setting up a state provider for a CRUD website. One issue I encountered is that when I navigate to www.mysite.com/posts/mypost, the URL gets shortened to www.mysite.com/mypost and does not trigger the controller as intend ...

Checking if a module is loaded through dynamic route code splitting

One issue with code splitting is that when the module is large, the initial loading time may result in a blank screen and delay for the user. function errorLoading(err) { console.error('Dynamic page loading failed', err); } fu ...

Adjusted position of the viewport if the DOM element containing the renderer is not located at the top of the display

I've come across an issue with a three.js scene within an Angular custom directive in the view. At the top, there's a navigation bar for switching between views (pretty standard so far). I set up a simple scene with a cube and basic camera rotati ...

Is it possible to duplicate native functions in JavaScript, such as window.alert or document.write?

I am looking to replace all instances of the alert function in my code with a custom alert message saying "you used alert". var hardCodedAlert = alert; //Although I understand this won't work. What other approach can I take? window.alert=function(){ ...

Utilizing Directives for DOM Manipulation in AngularJS

At this moment, I have a functional Angular app that is working properly. However, I am currently performing DOM manipulation within my controller instead of utilizing directives as recommended. My concern is, how can I correctly implement this functionali ...

Encountering an Uncaught TypeError when attempting to set properties of null with useRef

I've been working on an app that requires access to the user's device camera in order to display live video on the screen. I've managed to achieve this by utilizing a video HTML Object and setting the media stream as its srcObject. Everythin ...

What is the best way to create a map in React that allows for changing the state without affecting all elements?

When working with a JSON file containing various values, one of them being "iframe" which can hold either "si" (yes) or "no" based on whether it should include an iframe. With this value (yes/no), I need (this.props.tabsiframe === 'yes') to deter ...

Show the entire phrase within a Bootstrap modal with the help of jQuery, instead of just the initial

I have a PHP script that fetches a name from a database, and I'm using jQuery to display that name as HTML inside a Bootstrap modal when a button is clicked. However, the issue I'm facing is that when the name contains spaces, only the first word ...

Pug template syntax for importing JavaScript files with links

My Programming Dilemma In my NodeJS webserver setup, I use Express and Pug to serve HTML and JavaScript files. Here's a snippet of how it looks: index.pug html head title Hello body h1 Hello World! script(src='s ...

Running two different wdio.config.js files consecutively

Is it possible to run two wdio.config.js files with different configurations, one after another? Here is how the first configuration file is defined in the code: const { join } = require('path'); require('@babel/register') exports.co ...

Using jQuery's ajax function to send data with a variable name of data field

I am trying to figure out how to dynamically add a variable to the name of the data field I want to send information to through ajax. Below is an example of the code I'm working on: var qty = $('#qty_'+value).val(); $.ajax({ url: &apo ...

Passing Props to a Functional Stateless Component in React

The AuthLinks component should display the notification count passed as the notificationCount prop in the Notifications Component. I need to access the notificationCount value in the AuthLinks component, but it seems like it should be available in AuthLin ...

The closing tag for the "body" element was excluded even though OMITTAG NO was specified

While attempting to validate my contact support page, I encountered the following errors: Omission of end tag for "body", even though OMITTAG NO was specified ✉ You may have forgotten to close an element or intended to self-close an element by ending ...

I encountered login issues when trying to access dist/index.html in my Angular 8 application, but I found a workaround by utilizing the proxy.conf.json file, which

I have been trying to login through the index.html page in the angular 8 dist folder, but I keep encountering an error with the login API URL. Despite spending two days on this issue, I have not been able to find a solution. If anyone has any suggestions o ...

"Despite the null date in Node.js, the validation for expiration dates less than Date.now() is still being enforced

I am currently working on implementing validation for evaluating the finish status. However, my validation is encountering a problem with the "null" value of expiresAt. It should indicate that the evaluation has been successfully completed. The issue lie ...