簡體   English   中英

使用 Node.js 在該文件夾中生成文件夾和文件

[英]Generate folder and file within that folder using Node.js

我正在嘗試在該文件夾中生成一個文件夾和一個文件。 下面是我在 Node.js 中寫的 function。 問題是,我能夠生成文件夾,但文件生成不起作用。

有人可以幫忙嗎?

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!');
          }
        });
      }
    }
  );
};

這是您創建的目錄的路徑:

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

這是您將文件放入的目錄的路徑:

 `/${folderName}`

它們需要相同(除非當前工作目錄是文件系統根目錄,否則它們不會相同,這似乎不太可能)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM