1

I'm building a node js app(for learning) where i'm logging each operation into a file called log.txt

The logger module has the following code :

var fs = require('fs');

function write(data,filename)
{
  var entry = 'Time: '+new Date();
  if(filename !=null || filename != undefined) entry = entry+'\n\tFile: '+filename;
  if(data !=null || data != undefined) entry = entry+'\n\tMessage: '+data;
  entry = entry+'\n';

  fs.appendFile('./log.txt',entry,function(err){
      if(err){console.log('Log NOT Appended with data:\n\t'+entry);}
      else{console.log('Log Appended with data:\n\t'+entry);}
  });
}

exports.write = write;

now in my app.js i'm requiring it as :

var logger = require('./logger');

var fs = require('fs');

function ReadFile()
{

  var data = fs.readFileSync('./config.txt');
  if(data==null) 
          logger.write("Config data not found");
  else
      logger.write(data,"app.js");
}

ReadFile();

This throws me back an error saying :

 Object #<Object> has no method 'appendFile'

However this worked fine earlier on a different pc, I noticed this when tried to run my app on my system.

3
  • Can you share the version of node you're using on the failing pc? appendFile is a newer method available as of v0.8 Commented Mar 16, 2013 at 6:46
  • @michaelt I'm using v0.6.12, that might be the problem, i'll try updating and see if it works, thank you Commented Mar 16, 2013 at 7:08
  • yep that solved it, updated to v0.10.0 and now it works, thank you Commented Mar 17, 2013 at 9:38

1 Answer 1

1

This was a problem with the version, i was using v0.6.12 which did not have that method, i upgraded to v0.10.0 which solved it, thanks to michaelt for pointing it out.

Sign up to request clarification or add additional context in comments.

2 Comments

n00b question, but how do you upgrade to the latest version.
@jtromans you can use nvm(node version manager) or simply another module called "n" to upgrade and maintain versions of node on your system, for more details see this post - stackoverflow.com/questions/8191459/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.