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 7984991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:26:45+00:00 2026-06-04T11:26:45+00:00

How do I use nodejs to write a file, if the file is in

  • 0

How do I use nodejs to write a file, if the file is in a directory that may or may not exist?

It’s kind of similar to this question:

node.js Write file with directories?

Only I need a solution that creates the file while node-fs only makes directories.

  • 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-04T11:26:47+00:00Added an answer on June 4, 2026 at 11:26 am

    From FileUtils:

    Modify the functions to satisfy yours needs! But seriously, use a module instead of writing your own!

    createDirectory(): Creates a directory. If any of the previous directories that form the path don’t exist, they are created. Default permissions: 0777.

    File.prototype.createDirectory = function (cb){
        if (cb) cb = cb.bind (this);
        if (!this._path){
            if (cb) cb (NULL_PATH_ERROR, false);
            return;
        }
        if (!canWriteSM (this._usablePath)){
            if (cb) cb (SECURITY_WRITE_ERROR, false);
            return;
        }
    
        var mkdirDeep = function (path, cb){
            path.exists (function (exists){
                if (exists) return cb (null, false);
    
                FS.mkdir (path.getPath (), function (error){
                    if (!error) return cb (null, true);
    
                    var parent = path.getParentFile ();
                    if (parent === null) return cb (null, false);
    
                    mkdirDeep (parent, function (error, created){
                        if (created){
                            FS.mkdir (path.getPath (), function (error){
                                cb (error, !error);
                            });
                        }else{
                            parent.exists (function (exists){
                                if (!exists) return cb (null, false);
    
                                FS.mkdir (path.getPath (), function (error){
                                    cb (error, !error);
                                });
                            });
                        }
                    });
                });
            });
        };
    
        mkdirDeep (this.getAbsoluteFile (), function (error, created){
            if (cb) cb (error, created);
        });
    };
    

    createNewFile(): Creates a new file. Default permissions: 0666.

    File.prototype.createNewFile = function (cb){
        if (cb) cb = cb.bind (this);
        if (!this._path){
            if (cb) cb (NULL_PATH_ERROR, false);
            return;
        }
    
        if (!canWriteSM (this._usablePath)){
            if (cb) cb (SECURITY_WRITE_ERROR, false);
            return;
        }
    
        var path = this._usablePath;
        PATH.exists (path, function (exists){
            if (exists){
                if (cb) cb (null, false);
            }else{
                var s = FS.createWriteStream (path);
                s.on ("error", function (error){
                    if (cb) cb (error, false);
                });
                s.on ("close", function (){
                    if (cb) cb (null, true);
                });
                s.end ();
            }
        });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use node.js and socket.io. I have an application that runs on the IP
I'm trying to use Node.js to create a zip file from an existing folder,
I have seen coffescript tutorials that show how to use coffeescript with rails, nodejs,
This is my first question here on stackoverflow, so I hope that I am
I have a working HTML file with some javascript in it. I use NodeJS
I use simple script for writing in file between node.js process: var stream =
Ok so I have attempted to use multiple XML libraries that NodeJS have to
I understand how to use Protoype in standard Javascript with Node.js and modules, but
I am a node.js developer. I use Amazon ec2 to deploy my node.js apps.
A web app I am writing in JavaScript using node.js. I use Foreman, but

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.