Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Get Current Directory in Nodejs

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 214
    Comment on it

    In my previous blog, I have shared the insights about the step-by-step guide to open Node.js command prompt in Ubuntu. Later that evening, I received a query regarding ‘ways to get the current directory in the Node.js’.

    I thought of formulating another blog post for the same.

     

     

    Well let’s begin,

    We can get current directory name by using the following node global variable.

    _ _dirname

    It offers directory’s full path containing the present running scripts and returns the string value. It returns the entire resolved directory path that the presently executing script resides. If we type _dirname into path /d1/d2/myscript.js, it will return /d1/d2.

    It is the directory in which the currently executing script resides . If we type _dirname into path /d1/d2/myscript.js it will return /d1/d2.

     

    The below function will return current directory name:

    function getCurrentDirectory () {
    
          
        var path = __dirname.split('/');  // replace the "/" into "," in _dirname and convert into array
    
    
       var currentDIR = path[path.length-1]; // access into last element in array and paste into currentDIR
    
    
       return currentDIR;
    
    
     }

     

    That was easy, isn’t it?

    If you like the article, feel free to share it across the social media channels. If you have any suggestions regarding node js directory feel free to share them in the comments section below.

    How to Get Current Directory in Nodejs

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: