2

I'm trying to generate a folder and a file within that folder. Below is the function i wrote in Node.js. The problem is, I'm able to generate the folder but the file generation does not work.

Could anyone please help?

const createDir = (folderName) => {
  fs.mkdirSync(
    process.cwd() + '/' + folderName,
    { recursive: true },
    (error) => {
      if (error) {
        console.error('An error occured', error);
      } else {
        console.log('Your directory is made!');
        fs.writeFile(`/${folderName}/${folderName}.js`, '', (error) => {
          if (error) {
            console.error('An error occured', error);
          } else {
            console.log('file created!');
          }
        });
      }
    }
  );
};

1 Answer 1

2

This is the path to the directory you created:

process.cwd() + '/' + folderName,

This is the path to the directory you are putting the file in:

`/${folderName}`

They need to be the same (and they won't be unless the current working directory is the filesystem root, which seems unlikely).

Sign up to request clarification or add additional context in comments.

2 Comments

Don't you think they both are same as it gave me same result when I changed process.cwd() + '/' + folderName to process.cwd() + `/${folderName}`
I had to move my file creation step outside the else block to make it work. createDir(folderName); createFile(folderName);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.