简体   繁体   English

如何解决writeFile()的[错误:ENOENT:没有这样的文件或目录,打开]

[英]How to solve [Error: ENOENT: no such file or directory, open ] for writeFile()

this function should create a file, my script include 20 of them in a sequence 这个函数应该创建一个文件,我的脚本按顺序包含了其中的20个

const fs     = require('fs');

createFile( file.name + files.create_meta, blueprint.create_meta, data )

this function create the content 此功能创建内容

function createFile(filePath, blueprintPath, data) {
   const blueprint = fs.readFileSync(blueprintPath, 'utf8');
   const content = template(blueprint, {
     email: data.email,
     name: data.name,
     date: data.date,
   })

  fs.writeFile(filePath, content, function (err) {
    console.log(err);
  })
}

sometime it work, but often i get an this error message: 有时它可以工作,但通常我会收到以下错误消息:

 [Error: ENOENT: no such file or directory, open ] 

If this message appear, the file was not created because the files not exist at the beginning and the error means that the function cannot find a file in this path. 如果出现此消息,则说明文件没有创建,因为文件开头不存在,并且该错误表明函数无法在此路径中找到文件。

how can i guarantee that my function create the files? 如何保证我的函数创建文件?

You can update createFile function like this. 您可以像这样更新createFile函数。

const fs = require("fs");

function createFile(filePath, blueprintPath, data) {
  if (!fs.existsSync(blueprintPath)) {
    throw new Error(blueprintPath + " does not exist");
  }
  const blueprint = fs.readFileSync(blueprintPath, "utf8");
  const content = template(blueprint, {
    email: data.email,
    name: data.name,
    date: data.date
  });

  // creates an empty file if doesn't exist
  fs.closeSync(fs.openSync(filePath, "w"));
  fs.writeFile(filePath, content, function(err) {
    console.log(err.message);
  });
}

try {
  // call your function in try-catch
  createFile("./test.txt", "testtest", "test");
} catch (error) {
  console.log(error.message);
}




声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决错误:ENOENT:没有这样的文件或目录 - How to solve Error: ENOENT: no such file or directory 如何修复 nodejs 抛出错误; ^ 错误:ENOENT:没有这样的文件或目录,打开 - How to fix nodejs throw err; ^ Error: ENOENT: no such file or directory, open 详细堆栈错误:ENOENT:没有这样的文件或目录,打开 - verbose stack Error: ENOENT: no such file or directory, open Heroku错误:ENOENT:没有此类文件或目录,请打开“ .env” - Heroku Error: ENOENT: no such file or directory, open '.env' 错误:ENOENT:没有这样的文件或目录,打开尝试使用 fs 访问目录时 - Error: ENOENT: no such file or directory, open When trying to access a directory with fs 错误:ENOENT:没有这样的文件或目录 - Error: ENOENT: no such file or directory 错误:ENOENT:没有这样的文件或目录,打开“../config.json” - Error: ENOENT: no such file or directory, open '../config.json' Jasmine 报错:ENOENT: no such file or directory, open 'xmlresults.xml' - Jasmine report error: ENOENT: no such file or directory, open 'xmlresults.xml' 电子应用程序错误:ENOENT:没有这样的文件或目录,打开“/.env” - Electron app Error: ENOENT: no such file or directory, open '/.env' jsdoc错误ENOENT:没有这样的文件或目录,打开'./readme/readme.md' - jsdoc error ENOENT: no such file or directory, open './readme/readme.md'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM