Dive into Basic Concepts

This module may feel way longer compared to previously completed ones. Please don't rush it. These are the basic concepts that the complex ones are built on.

Learning Objectives

Console.log out

console.log in node is a web syntatic wrapper used for debugging and sending output that goes to your node's standard output stream, usually your terminal. It is a convenience alternative to the primitive process.stdout.write() function.

The console is a global instance configured to write to node's standard output among other things. This allows the use of .log() method without importing node:console at the top of the file.

If process.stdout confuse you, don't mind it. You will see more lengthier explanation later.

Let's run some examples in the REPL.

bash$ node
Welcome to Node.js v21.6.0.
Type ".help" for more information.
> console.log("kelvin")
kelvin
undefined
> process.stdout.write('kelvin \n')
kelvin
true

In subsequent examples and the rest of the course, I'll skip these terminal details and jump straight at the sample code and their expected outputs so that the example above is rewritten with fewer lines.

bashconsole.log("kelvin") // kelvin
process.stdout.write('kelvin \n') // kelvin

Command arguments with process.argv

Node programs can read its command-line arguments from process.argv. Process.argv returns an array that contains the commands passed when the Node.js process was launched. The first element of this array will be the path to the Node executable (process.execPath), and whatever follows your node command.

Run this in a REPL

bashprocess.argv // [ '/path/to/yours/versions/node/v21.6.0/bin/node' ]

Let's explore an example in a script. Save this Node program to a file, argv1.js:

bash//file: argv1.js
console.log(process.argv)

Run this command in the parent directory from where the argv1.js file is located.

bash$ node argv1.js

/**
[
  '/path/to/yours/versions/node/v21.6.0/bin/node',
  '/path/to/folder/argv1.js'
]
*/
A couple of things to keep in mind
  1. By default when you run node via script method the second argument is always the script name.

  2. The first and second elements of process.argv will be fully qualified filesystem paths to the Node executable and the file of JavaScript that is being executed.

  3. Command-line arguments that are intended for and interpreted by the Node executable itself do not appear in process.argv array.

    Arguments not in process.argv

    Arguments intended for the Node runtime (and not your script) don't appear in the process.argv. Node parses and use them before loading the JavaScript file and do not need to pass them to your program. Examples are --inspect, --require etc

    FlagPurpose
    --inspectEnable the debugger
    --requirepreload a module before running your script

    If you want to access all arguments including those used by node, use process.execArgv.

Environment variables with process.env

Node programs can be given special variables, Unix-style environment variables. They are available through process.env.

Unix-style environment variables?

We could have written that sentence as: Node programs can also take input from environment variables. The adjective Unix-style is to emphasize the acceptable style when writing that specific style of variables, environment variables.

Some of the standard that guides this style are:

  1. All character must be written in UPPERCASE. It's the name convention and the most visible characteristics of this style.
  2. All values of a variable must be stored as strings (even numbers). No real data types like int, bool ecosystem
  3. No space in variable assigment. i.e between the KEY and VALUE e.g count = 42 is wrong; count=2 is the right way.

process.env returns an object, whose property are the environment variable names and their values. It contains your computer's environment variables and that defined in your current node's instance.

process.env is important for configurations. It is used to store variables that might change between deployments (local and production), hides sensitive information that would have otherwise be hardcoded into the codebase.

On the REPL, run process.env. It output a long object.

To get our hands dirty, create some custom variables in a .env file relative to the folder you're running the node process from.

bash//file: .env
echo 'PORTAL=SEKX_10001' > .env

In the REPL relative to the .env folder run process.env. Try find your newly defined variables. Or better still do process.env.PORTAL. Only that you wouldn't find PORTAL in the output and process.env.PORTAL returns undefined.

You would manually load the env file if you want the variables to be included in your system's environment.

Start the REPL with node --env-file=.env. This loads the .env file in your REPL environment and add it to the rest of your environment variables. process.env.PORTAL now returns "SEKX_10001". Celebrationssss 🍕

Modules and npm (Node Package Manager)

Node had its module system before Javascript's'. This system is called CommonJS.

The then Node's module system uses the require() function to import values into a module. Available to other modules through its exports (same as module.exports) property.

In javascript, a module is typically a file. Not all programming languages behave this way. For some, multiple modules can exist in a single file.

What is a module?

In recent versions, after JavaScript rolled out it own module system, we now have 2 combined module systems available for us. This later module system is called ES6 module, and the earlier one is Commonjs module. These modules are not compatible so you are forced to stick with one in your project. The recommended one is ES6 module except you are working in a legacy and large codebase written in CommonJS.

Common js

bash// calc.js
const add = function(a, b){
  return a  +  b;
}
exports.add = add;

// main.js
const { add } = require('./calc.js');

console.log(add(2, 4));

ES6 Module

bash// calc.js
export const add = function(a, b){
  return a  +  b;
}

// main.js
import { add } from './calc.js';

console.log(add(2, 4));
A little deep on module and extensions

The simplest way to tell Node what kind of module it is loading is to encode this information in the file extension. If you save your JavaScript code in a file that ends with .mjs, then Node will always load it as an ES6 module, will expect it to use import and export, and will not provide a require() function. And if you save your code in a file that ends with .cjs, then Node will always treat it as a CommonJS module, will provide a require() function, and will throw a SyntaxError if you use import or export declarations.

For files that do not have an explicit .mjs or .cjs extension, Node looks into the package.json file in the same directory as the file. Once the nearest package.json file is found, Node checks for a top level type property in the JSON object. If the value of the type property is "module", then Node loads the file as an ES6 module. If the value of that property is "commonjs", then Node loads the files as a CommonJS module. Note that you do not need to have a package.json file to run Node programs: when no such file is found (or when the file is found but it does not have a type property), Node defaults to using CommonJS modules.

Node Manager Packager

On installing node, it is accompanied with the Node Package Manager, NPM. NPM is the official package manager for the JavaScript ecosystem.

NPM downloads and manage libraries you would be using in your code. It keep tracks of these dependencies (as well as other info about these dependencies) in files package.json and package-lock.json in the root directory.

Interesting Read [1]: Libraries or API;

We aren't covering npm in detail here. You'll have many opportunity to learn about this manager as you go especially since you will most likely not build anything without it.

Closing

Although we have covered a lot in this chapter, we have only scratched the surface of basic node concepts. There are many more concepts to learn and understand.

See this list for more:

  1. Asynchronous and event driven
  2. Core modules

If you have any questions, please ask them in the course discussion forum. If you are not sure how to do this, please refer to the learn guide.

This page was published on 18/06/2025, last updated on 15/02/2026. Contributed to by kelvinsekx.