myTest532 myTest532
myTest532 myTest532

Reputation: 2381

Open XML file in NodeJS in the same folder - No such file

I'm trying to open an XML file that is in the same folder of my controller. I have the files:

controllers/test/myController.js controllers/test/myXML.xml

The myXML.xml:

<?xml version="1.0"?>
<something>

</something>

myController.js file:

exports.getXML = async (req, res, next) => {
    const xmlFile = fs.readFileSync('./myXML.xml', 'utf8');
    console.log(xmlFile);
}

I'm getting the error:

UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open './myXML.xml'

I tried to change './myXML.xml' to 'myXML.xml' and I also get the same error.

Upvotes: 0

Views: 151

Answers (1)

Quentin
Quentin

Reputation: 944114

Paths are resolved relative to the current working directory, not the location of the module.

You can use __dirname to get the path to the directory containing the module, then you can append your filename to that.

Upvotes: 2

Related Questions