Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Read File From %appdata%

I am running node and I want to read a file from the %appdata% folder and I would rather not hard-code that path.

This is basically what I have so far:

//...require(some things)
var fs = require('fs');

var fileData;
try{
    fileData = fs.readFileSync('%appdata%/folder/file.txt',{encoding:'utf8'});
}
catch(e){
    console.log(e);
    fileData = 42; //default value
}

//... app.get(some things)
//... app.listen

When I run this, I get the message:

{ [Error: ENOENT, no such file or directory 'C:\projectdirectory\%appdata%\folder\file.txt']

  errno: -4058,

  code: 'ENOENT',

  path: 'C:\projectdirectory\%appdata%\folder\file.txt',

  syscall: 'open' }

How do I get it to recognize the %appdata% variable?

like image 376
DeadEli Avatar asked Aug 14 '15 14:08

DeadEli


1 Answers

You have to get the value from process.env instead:

fileData = fs.readFileSync(process.env.APPDATA + '/folder/file.txt',{encoding:'utf8'});
like image 106
mscdex Avatar answered Sep 26 '22 19:09

mscdex