Not works fs.readFile in node js

后端 未结 5 803

I have:

 fs.readFile(\'../services/Prescipcion.xml\', \"utf8\", function (err, data) {
    console.log(\"err->\", err);
    console.log(\"data\", data);
 });
         


        
5条回答
  •  余生分开走
    2021-02-19 11:03

    The error message says no such file or directory, so at first sight this most likely means the path to the file is incorrect.

    Either the filename is incorrect (typo?) or the directory is incorrectly resolved. Take note that a relative path will be resolved against process.cwd():

    process.cwd(): Returns the current working directory of the process.

    You can try using console.log(process.cwd()) to help you debug the issue.

    If the file Prescipcion.xml should be retrieved locally from where the script is run, you can also use the following construct:

    fs.readFileSync(path.join(__dirname, '../services') + '/Prescipcion.xml', 'utf8');

    __dirname: The name of the directory that the currently executing script resides in.

提交回复
热议问题