2

The Structure of my project is:

- project
  - routes
     admin.js
     sql-requests.xml

Now, in my admin.js I am trying to read the sql-requests.xml file. Assuming they are on exactly the same path, this is my code:

var fs = require('fs');
var content = fs.readFileSync('./sql-requests.xml');

This has failed. Here's the list of what I have attempted:

  • sql-requests
  • sql-requests.xml
  • /sql-requests.xml
Pero P.
  • 25,813
  • 9
  • 61
  • 85
Bravo
  • 1,944
  • 4
  • 29
  • 53

1 Answers1

6

This will depend on the working directory where you are executing admin.js from. If you're looking to access the file next to the current executing script then the following will be more reliable:

var content = fs.readFileSync(__dirname + '/sql-requests.xml');

See this answer (and question) for more details: https://stackoverflow.com/a/18283508/112196

Community
  • 1
  • 1
Pero P.
  • 25,813
  • 9
  • 61
  • 85
  • Thank you Pero! In case the location of my file will be one level lower, how can I read it? Assume the file is in project folder, not routes – Bravo Mar 18 '15 at 18:38
  • To go up use `__dirname + '../sql-requests.xml'` or down `__dirname + 'routes/sql-requests.xml'` for example – Pero P. Mar 18 '15 at 18:43