Creating folder and file using remix js

42 views Asked by At

I'm trying to create a folder and a file inside that folder from my Remix js app. So after certain action, I did the following but this doesn't work from my Remix app.

But it works as expected when I create a sample.js file outside of the remix project and run the sample.js file.

How can I fix this issue?

const fs = require('fs');
const path = require('path');

// Define the folder directory. 
const newFolderPath = path.join("./", 'newFolder');

// Define the file directory. 
const newFilePath = path.join(newFolderPath, 'newFile.txt');


// Function to create new folder and file
async function createFolderAndFile() {
  try {
      // Create a new folder
      await fs.promises.mkdir(newFolderPath);

      // Create a new file within the folder
      await fs.promises.writeFile(newFilePath, 'Hello, World!', 'utf8');

      console.log('New folder and file created successfully.');
  } catch (error) {
      console.error('Error creating folder and file:', error);
  }
}

createFolderAndFile();

Thanks!

0

There are 0 answers