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
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"));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"));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");
EventEmitter
Definition: The EventEmitter class lets objects announce events.
Syntax:
EventEmitter
Example:
const EventEmitter = require("events");
const e = new EventEmitter();on
Definition: Listen with on and trigger with emit .
Syntax:
on
Example:
e.on("greet", function (n) { console.log("Hi " + n); });emit
Definition: Listen with on and trigger with emit .
Syntax:
emit
Example:
e.emit("greet", "Asha");async
Definition: Use callbacks, promises, or async functions with await to write it in order.
Syntax:
async
Example:
async function main() {await
Definition: Use callbacks, promises, or async functions with await to write it in order.
Syntax:
await
Example:
await wait(100);
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) {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");searchParams.get
Definition: searchParams.get reads a value from the query string.
Syntax:
searchParams.get
Example:
console.log(u.searchParams.get("qty"));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}');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 }));process
Definition: The global process object describes the running program.
Syntax:
process
Example:
console.log(process.argv.length); console.log(typeof process.cwd());
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());
catch
Definition: Wrap risky code in try and catch .
Syntax:
catch
Example:
} catch (e) {name
Definition: The error object has a name and a message .
Syntax:
name
Example:
console.log(e.name);
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"]);
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; });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); });Example code
console.log("Hello from Node.js!");