Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:03:59+00:00 2026-06-05T09:03:59+00:00

Just testing NodeJS out and still learning to think in javascript, how can I

  • 0

Just testing NodeJS out and still learning to think in javascript, how can I get this basic FileIO operation below to work?

Here’s what I’d like it to do:

  • Read XML file (read into memory)
  • Put all contents into a variable
  • Write XML file from variable
  • Output should be the same as original file
var fs = require('fs');
var filepath = 'c:\/testin.xml';

fs.readFile(filepath, 'utf8', function(err, data) {
    if(err) {
        console.error("Could not open file: %s", err);
    }
});

fs.writeFile('c:\/testout.xml', data, function(err) {
    if(err) {
        console.error("Could not write file: %s", err);
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:04:01+00:00Added an answer on June 5, 2026 at 9:04 am

    The problem with your code is that you try to write the data you read to the target file before it has been read – those operations are asynchronous.

    Simply move the file-writing code into the callback of the readFile operation:

    fs.readFile(filepath, 'utf8', function(err, data) {
        if(err) {
            console.error("Could not open file: %s", err);
            return;
        }
        fs.writeFile('c:/testout.xml', data, function(err) {
            if(err) {
                console.error("Could not write file: %s", err);
            }
        });
    });
    

    Another option would be using readFileSync() – but that would be a bad idea depending on when you do that (e.g. if the oepration is caused by a HTTP request from a user)

    var data = fs.readFileSync(filepath, 'utf-8');
    fs.writeFileSync('c:/testout.xml', data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just testing out some basic networking code written in Visual C++. I have
I was just testing this download script below. The downloading works fine but the
I am still very new with chrome extensions and am just testing things out.
I am Just testing this code to stop form by submitting... Jquery Part... $(#request_quote).submit(function(e){
i'm just testing out the csv component in python, and i am having some
I am just testing an app to get data off our web server, previously
I'm new in QT, and I'm just testing out the MOC. For a given
So, I am just testing NSNotifications on a variety of cases and this one
This is driving me crazy. Just testing a site on IE9 and discovered that
I just installed Ubuntu 10.10 server with NodeJS 0.4.6 using this guide: http://www.codediesel.com/linux/installing-node-js-on-ubuntu-10-04/ on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.