read my full post on Buffer.
This will give you the current dir of the module that you are working with, very useful when working with paths.
Another useful paths related variable that is the full path to the current module, including it’s filename.
This is a way of exporting methods in a module to be used in another module by way of using require.
for example if I have a module.js file like this:
I can use that method in an index.js file in the same folder
So when I call index.js with node the method will work in index.js
This is of course a way o go about breaking your code down into smaller, more manageable sections of code. It is essential to the process of defining a reusable package that can be used across projects, rather than writing the same code over, and over again from the ground up.
exports is what I generally used if I want to make a whole bunch of methods that are exported, If I just have one method to export there is also module.exports that is also of interest. More on that later.
This represents the current module or *.js file in your project, and contains helpful info on it. In my experience so far this variable has two major uses:
One is to find out of the current module has been called directly from the Command Line Interface, or is being used by another module, by making a comparison to require.main.
Doing something like this is necessary when making any kind of module that will be used directly, or by way of another module, or for whatever reason I just want to know how the module is being used.
The other Important use of the module variable is the module.exports property, this is what I use in conjunction with exports, to define what is exported when the module is used my another module with require.
The process variable contains a whole bunch of useful information, such as any environment variable that may be present in the operating system environment, the current working directory, and any arguments that may have been given to the module from the Command Line Interface if the module was called directly rather than from a script.
This is where any arguments that have been given at the command line will be, it is an array, and the first two arguments will always be node, and the module that is beging called, that is why you often see something like this:
You may want to use some kind of option parser in stead of working out how to handle the parsing of options that are given from the command line from scratch. What I have experence with so far is nopt, but there are many others out there.
This method will return the current working directory. Say you want to write a script that will read a file of a certain type at any directory at which the script is called, this is one way to anyways find out what the directory is
This is another very important must know method in the process variable. It will contain all the environment variables that are available in the operating system in which the script is running, which becomes important when deploying an application to a hosting company that will only allow an application to start a server on a certain port that is specified by way of an environment variable.
This will set the value of port to a port given by way of an environment variable called PORT if present, else it will expect a port value to be given as the first argument, and if that is not given it will default to the hard coded value 8080;
Require is what is used to use additional modules in a module. It can be a built in node.js module, such as path, or fs, it can be a package that has been installed into a node_modules folder by way of using npm such as cheerio, or a module of your own design that may be within a lib folder in your project.
basic use of require to work with the node.js path, and file-system modules.