codegurukool.com · Free coding tutorials & online compiler

Home › Node.js Tutorial › All commands

All commands

Every command of this course: name, detailed description, syntax and example code.

Every command used in this course, with its name, a detailed description, the syntax and an example.

require

Definition: Loads a module (a core module, a package or your own file) and returns what it exports.

Syntax:

const name = require("module");

Example:

const fs = require("fs");

module.exports

Definition: Chooses what a file shares with other files that require it.

Syntax:

module.exports = value;

Example:

module.exports = { add };

fs.readFileSync

Definition: Reads a whole file and waits until it is finished. Pass "utf8" to get text instead of raw bytes.

Syntax:

fs.readFileSync(path, "utf8");

Example:

const text = fs.readFileSync("a.txt", "utf8");

http.createServer

Definition: Creates a web server. The function you pass runs for every request.

Syntax:

http.createServer(function (req, res) { ... });

Example:

http.createServer(function (req, res) {
  res.end("Hi");
}).listen(3000);

process.argv

Definition: An array of the command-line arguments. The first two items are node and the script path.

Syntax:

process.argv

Example:

node app.js hello  ->  process.argv[2] is "hello"

npm install

Definition: Downloads a package from the npm registry and records it in package.json.

Syntax:

npm install package-name

Example:

npm install express

app.js

Definition: Save code in a file such as app.js and run it with node app.js .

Syntax:

app.js

Example:

// Terminal: node app.js

↗ Node.js get started

path

Definition: Core modules need no install: path works with file paths, os reads system information and util offers helpers.

Syntax:

path

Example:

const path = require("path");
console.log(path.basename("/a/b/file.txt"));
console.log(path.extname("file.txt"));

↗ Node.js built-in modules

fs

Definition: The fs module reads and writes files.

Syntax:

fs

Example:

const fs = require("fs");
fs.writeFileSync(p, "Saved text");
console.log(fs.readFileSync(p, "utf8"));

↗ Node.js file system

fs.writeFileSync

Definition: fs.writeFileSync and fs.readFileSync block until done; the promise version fs.promises does not.

Syntax:

fs.writeFileSync

Example:

fs.writeFileSync(p, "Saved text");

↗ Node.js file system

EventEmitter

Definition: The EventEmitter class lets objects announce events.

Syntax:

EventEmitter

Example:

const EventEmitter = require("events");
const e = new EventEmitter();

↗ Node.js events

on

Definition: Listen with on and trigger with emit .

Syntax:

on

Example:

e.on("greet", function (n) { console.log("Hi " + n); });

↗ Node.js events

emit

Definition: Listen with on and trigger with emit .

Syntax:

emit

Example:

e.emit("greet", "Asha");

↗ Node.js events

async

Definition: Use callbacks, promises, or async functions with await to write it in order.

Syntax:

async

Example:

async function main() {

↗ Node.js async and await

await

Definition: Use callbacks, promises, or async functions with await to write it in order.

Syntax:

await

Example:

await wait(100);

↗ Node.js async and await

http

Definition: The http module builds servers.

Syntax:

http

Example:

const http = require("http");
const s = http.createServer(function (req, res) {
http.get({ port: s.address().port }, function (r) {

↗ Node.js HTTP server

URL

Definition: The URL class splits an address into parts.

Syntax:

URL

Example:

const u = new URL("https://example.com/shop?item=pen&qty=3");

↗ Node.js URL and query strings

searchParams.get

Definition: searchParams.get reads a value from the query string.

Syntax:

searchParams.get

Example:

console.log(u.searchParams.get("qty"));

↗ Node.js URL and query strings

JSON.parse

Definition: JSON.parse turns JSON text into an object and JSON.stringify turns an object into text.

Syntax:

JSON.parse

Example:

const o = JSON.parse('{"name":"Asha","age":20}');

↗ Node.js JSON

JSON.stringify

Definition: JSON.parse turns JSON text into an object and JSON.stringify turns an object into text.

Syntax:

JSON.stringify

Example:

console.log(JSON.stringify({ ok: true }));

↗ Node.js JSON

process

Definition: The global process object describes the running program.

Syntax:

process

Example:

console.log(process.argv.length);
console.log(typeof process.cwd());

↗ Node.js process

process.cwd

Definition: process.argv lists the command-line arguments and process.cwd returns the current folder.

Syntax:

process.cwd

Example:

console.log(typeof process.cwd());

↗ Node.js process

try

Definition: Wrap risky code in try and catch .

Syntax:

try

Example:

try {

↗ Node.js errors

catch

Definition: Wrap risky code in try and catch .

Syntax:

catch

Example:

} catch (e) {

↗ Node.js errors

name

Definition: The error object has a name and a message .

Syntax:

name

Example:

console.log(e.name);

↗ Node.js errors

Readable.from

Definition: Readable.from makes a stream from an array, and the data and end events deliver the pieces.

Syntax:

Readable.from

Example:

const r = Readable.from(["a", "b", "c"]);

↗ Node.js streams

data

Definition: A stream handles data in chunks, so big files do not fill memory.

Syntax:

data

Example:

r.on("data", function (c) { out += c; });

↗ Node.js streams

end

Definition: Readable.from makes a stream from an array, and the data and end events deliver the pieces.

Syntax:

end

Example:

r.on("end", function () { console.log(out); });

↗ Node.js streams

Example code

console.log("Hello from Node.js!");

Run this code in the online editor

← Welcome to the Node.js courseNode.js introduction →