0% found this document useful (0 votes)
2 views

2. Globals of NodeJS

Blah blah

Uploaded by

Smoked By Cylien
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2. Globals of NodeJS

Blah blah

Uploaded by

Smoked By Cylien
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Globals of NodeJS

Table of Content –
1. Globals.
2. What is module pattern?
3. How to create our own modules?

1. Globals -
NodeJS provides us a lot of globals. These are global variables that are available
everywhere and in every part of your code in NodeJS runtime. These globals are important
for a lot of features.

There are 5 important globals:

1. process:
Process is an object that contains all of the current processes that are running inside
your machine. It contains all sorts of information’s related to your Operating System,
for example:
`arch: “x64”` which is telling that the current machine I am using is based on 64-Bit
based architecture processor.
`platform: “win32” is telling that I am currently using windows 32-Bit Operating
System.
And there are a lot more inside this process object.
2. __dirname:
Using `__dirname` functions, which returns a pointer to a string that is a path name
of the parent directory of that file.
3. require:

The `require()` function helps us to require other modules inside our separate files.

4. global:

While manipulating DOM, you must have heard about the window object inside
browsers. The window object helps us to access a lot of things such as the `on`
events, alerts, and the document object which contains all of the DOM
manipulation functions. These are globals that browsers provide. Similar to window
object, there are globals that NodeJS provide.

5. module:

Using module object, we can access the module pattern of NodeJS. Modules are
encapsulated units of code that can be reused across different parts of an
application. Modules help us organize code in to smaller, manageable piece of
chunks. Modules promote code reusability, and facilitate better maintainability and
scalability of NodeJS applications.

*There might be some cases where some of the globals might not be available. For
example, there are cases where you cannot use `__dirname` every time, there will be
certain sets of configurations that you will be able to do in which `__dirname` will not be
available. *

When you run any piece of code in VS Code while using `node (file name)` command, you
will see there written `node (complete path of your file)`. Which means that we can access
NodeJS globals functions inside VS Code. Let us try logging out some of the globals and see
what actually are they:

• process: When we log process, we will see process is a very large object which contains all
the information of the current process that are running on your machine.
• process.env: If you scroll a bit then after logging out process you will see an `env`
object, this contains all of the environment variables of your system.
• module: After logging module, you will see that module is an object, this module
global helps you to access the module pattern of NodeJS. Inside the module object
you may see `exports` empty object, this is important for future references.
• require: When you run the log, you will see `require()` is a function.
• __dirname: This function returns the parent path from where you are executing the code.
• global: The `global` is an object which contains timers and fetch functions, etc.

2. What is module pattern?


Module is a mechanism for splitting JavaScript programs in to separate manageable chunks
called as module, that can be imported whenever needed.
You might have heard about the D.R.Y (Don’t Repeat Yourself) principal. In D.R.Y. principal
we technically try to create functions if there is something that can be repeated again and
again, so you try to bind the logic inside a function.

Let us assume that you are trying to prepare a library or bunch of functions but some set of
functions are related to one task, some set of functions are related to other task, so, what
we can do is, we can segregate out our piece of code in more manageable chunks that we
call as Module.

There are two ways for creating such Modules:

1. CJS: CJS, abbreviated as CommonJS Modules, is an old mechanism or the traditional


mechanism for creating Modules.
2. ESM: ESM, abbreviated as ES6 (EcmaScript 6) Modules.

The ReactJS supports ES6 Modules only, but an old ExpressJS project had CommonJS
Modules.
Now ExpressJS supports ES6 Modules as well.

CJS Modules and ESM Modules.


3. How to create our own modules?
There is on file called as linearSearch.js and inside it, we have a logic to search the array
and return the value x position.
// linearSearch.js const
search = (arr, x) => {
for (let i = 0; i < arr.length; i++)
{ if (arr[i] == x) {
return i;
} }
return undefined;
}

This function is created just to demonstrate.

Now we have another file called as index.js, and inside index.js we want to use this
`search()` function which we have created inside linearSearch.js file. So, in order to do that,
we have a NodeJS global called as `module`, `module` is an object and it contains a key
called as `exports`, this `exports` is an empty object. If we want to make any piece of code
to become a global, so we can access it globally in our project, we use `module.exports`
object.
// linearSearch.js
module.exports = {
search: search
}
Exporting a function from linearSearch.js file.

We targeted the `exports` empty object and filled it with a `Key Value` pair. Now this
function has been saved inside `module.exports` object, and it is not empty anymore.

If the key and value are basically same, then we can also do:
// linearSearch.js
module.exports = {
search
}
Behind the scene JavaScript will read it as `search: search`.

The function has been exported successfully, now we will see how we can access or import
the function inside our index.js file.

In order to import a function then there is a NodeJS global function called as `require()`,
using which we can import the function which we have just exported:
// index.js const searchAlgo =
require(“./linearSearch”);
// The `searchAlgo` is the name of the variable which we are creating. //
After using `=` we are using `require()` function and inside `()` we are
giving path of the file from where we exported the function.

Importing, or in NodeJS term, requiring other exported functions.

We have successfully imported or in NodeJS we call it `require`, so we required our


exported function inside index.js

In order to use the function that we have just required inside our index.js file, we can use it
like this:

console.log(searchAlgo.search([1,2,454,5,23,12,4,5], 454));

The `searchAlgo` variable that holds the value of require has become an object. As I said,
the `exports` inside `module` object, the `module.exports` is an empty object, but after
exporting the function, the exports object is containing the function inside it. For visual
guidance:

// Before it was empty.


exports: {

// After updating the object with search function. exports:


{
search: search
}
module.exports{} object, before and after exporting.

So in order to access the search function from any object, we use `dot`. That is exactly what
we are doing.
const searchAlgo =
require("./linearSearch");

The `searchAlgo` is just a variable name, and that variable is holding an object after it has
been imported.

Now when we want to use this `searchAlgo` function, then we will have to do:

console.log(searchAlgo.search([1,2,454,5,23,12,4,5], 454));

You might also like