One thing that I wish that I got solid early on when I was first starting out with nodejs is how to get the current working directory, and also how to always get the directory where a script is located, along with other typical paths of interest on a system. In the process global there is a cwd method that when called will return the current working directory, which is of course a major directory of interest when it comes to creating a nodejs script. However it is not the only directory of interest of course it is also important to know how to go about getting the directory of the current script, and also how to get paths to assets that are relative to that script. There is also how to go about getting the user folder location when it comes to a standard location to park user specific data.
So in this post I will be covering a basic example of the process.cwd method of course, but I will also be touching base on a whole bunch of other little topics that revolve around that.
1 - Just a basic process.cwd example for now when it just comes to getting the Current Working Directory
If I just want to get the current working directory I can use the process.cwd method to do so. However it is important to remember that the current working directory is just that, the current directory in which I am working with something. It should not be confused with the __dirname global that will give me the path to the current script. More on that and many other little things in a bit, but for now lets just start out with the basic get current working directory example.
So for a very basic example of the process.cwd method I can have a basic.js file where I will just be passing the value of process.cwd to the console.log method. When I run this basic.js file the current working path will be spit out to the standard output.
So say I save the about as basic.js in my home folder I can then call it like this
As expected if I change the current working path to something like the \/usr\/lib folder on Linux systems, I can then call the script like this
And the result will not of course be the \/usr\/lib folder, however what if I also want to get the folder where the script is located each time? Well this is where other values and methods of interest come into play so lets look at a few more examples of this sort of thing.
There is also getting the home folder of the current user, and doing so in a standard nodejs way that will work in many different operating systems. I could work out my own solution for parking things in a system, but why bother with that when there is the os.homedir method that will give me what I want with this when it comes to typical use.
The home directory is a good location to park things like settings that should be set on a user by user basis. if of course is also a good location to create a folder that will be used to store a users work when it comes to creating some kind of project that will be used to create something like blog post files, or any other kind of asset depending of course on the nature of the project that is begin made with nodejs.
I often will want to create new paths that are relative to the current working directory, or the directory in which the current source code script is located. Say for example I want to get a script that should be located in a lib folder relative to the location of the current script. To get that path I will need to combine the value of __dirname with a string value that is the rest of the path that will resolve to an absolute path to that script that I want. For these kinds of tasks there is the path module, and for something to this effect there is the path.resolve method to be specific about it.
There is a great deal to get into when it comes to the paths module. There are many other useful methods to be aware of with this mode that have to do with similar tasks like getting the base folder name of a path the contains a file name for example. It is worth checking out in depth if you have not got around to doing so yet.
So the process.cwd method is what there is to use as a standard way to go about getting the current working path. It is a good idea to use it as a way to show what the intention is, and to use it with path module methods to get any paths I might want when it comes to things that should be relative to the current working folder rather than the source code folder that may, and often should be, in another location on a system.
If you are looking for more to read on the process global, then maybe check out my main post on the nodejs process global. I keep coming back to it now and then when I get around to editing and creating new posts like this.