Table of Contents
Getting started with NodeJS the right way empowers you to exploit the potential of NodeJS. This tutorial walks you through NodeJS overview, mainly focusing on imparting the mindset needed to fully and creatively apply NodeJS.
We may interchange the terms like Node, Node.js, or NodeJS to mean the same thing.
Let's do this!
Getting started with NodeJS by understanding its origin
Getting started with NodeJS processes
Similarly, the (node) wrapper is bundled with functionalities to interact with the operating system.
Node attempts to mirror the browser API. Several APIs like
setInterval() work similarly in both the browser's window and Node's global object.
You can see the libraries by entering the REPL and double-tapping the tab key.
You can also use Node as a CLI tool. Check all you can do in Node as a command-line tool by typing
node followed by the
For example, you can execute commands and print the result using the
-p flag. For instance, you could view the current date by running the following command.
node -p "new Date()"
What happens when you run a Node command?
Running a node command initializes an operating system process. Node.js continues to interact with the operating system process through its built-in
process object. The
process object thus becomes the interface between your node environment and the operating system environment.
The interface reads and writes into the operating system at the low level. At a high level, you write Node.js programs, which are sent to the process object. You can also change the behavior of Node by setting the environment variables.
You need a code editor to write commands. For example, I am using Visual Studio Code in this tutorial. Most code editors come with a terminal for viewing the program output.
Next, create a file ending in the
.js extension and open it with the installed code editor.
Unlike the REPL, where you write commands and see the output instantly, writing a Node script requires bundling the input in the process object. For example, we can see the output of the date object by wrapping the command in the
We convert the output to a string because the
write() method expects a string. Better yet, we can use the high-level
console object with the
log() method to print the output.
Getting started with NodeJS modules
Node.js wraps your code in an immediately invoked functional expression (IIFE). The function accepts five arguments:
module, `filename`, and `dirname`.
exports, an alias to
module.exports, is a mutable object. Each file has a unique
module.exports object that wraps the file's code, enabling you to create an interface between two files: exporting and importing files.
The file being exported and imported is called a
module. There are three main types of modules in Node.js: built-in, installed, and custom modules.
Getting started with NodeJS built-in modules
Built-in modules come with Node.js upon installation. You can print the built-in modules by double-tapping the
tab key or console-logging the limited
_builtinLibs property of the
Some of the frequently used Node's built-in modules are:
- fs: short for the file system and is used to read and write files.
- path: enables you to work with folders and file paths.
- events: controls the output if something happens. The concept of events plays a massive role in Node traits like the non-blocking I/O. and most modules' operations like the
- http : creates a web server.
Getting started with NodeJS installed and custom modules
You can install modules through the Node Package Manager (
npm) or its alternatives like Yarn and Node Version Manager (
Lastly, you can create a custom module by writing a file and exporting the contents.
You import the module using Node's native
A custom module requires you to specify the path, whereas you only need to specify the package name when importing built-in and installed modules.
With the constantly changing Node versions, it is now possible to use the ECMAScript's
export syntax in Nodejs.
You start by initializing your package as an NPM package.
npm init # OR npm init -y
type key with the
module value. This time around, you should introduce the
.js extension when importing files.
__dirname returns the full path to the current folder hosting your (current) file. Use
__filename instead to include the file name in the output.
Getting started with NodeJS packages
On initializing the NPM, a
package.json file is created. A package describes the files you need for an installed module.
You can install or publish packages with NPM. Apart from managing the packages, NPX executes a package. The most immediate alternative to NPX and NPM is Yarn, whose uniqueness arises from its speed, consistency, and security.
The information in the
package.json file determines the behavior of your package. Although the must-have contents of the file are package
version, a standard package has
Scripts control how to run commands. Some of the default scripts are start, build, and test. You can run them without the
Otherwise, use the
run command if you create a custom script.
npm run [script name]
Dependencies tell Node the (installed) packages used in your application. The package primarily meant for local development is called a dev dependency, and NPM references it under the
devDependencies portion of the
NPM decides the type and location of a dependency according to how you install the package. For example, the
--save-dev flag, summarized as
-D, saves a package under
--global flag, summarized as
-g saves the package in your system and is available for subsequent application(s) usage. Otherwise, the package lands in
The packages are semantically versioned.
Whenever you import a module with a path, Node.js searches for the module's existence and configuration in the
You can delete the
node_modules folder and reinstate the module's configuration by running the NPM install command.
NPM goes to its repository or GitHub to fetch all the data needed for the target package to function.
Lastly, you can delete a package using the NPM uninstall command.
npm uninstall [package name]
After getting started with NodeJS, you should focus more on its core modules like
Next, learn how to create a web server with frameworks like Express.js as you continue exploring Nodejs libraries shown in this tutorial.