Search notes:

Node.js

Node.js bundles the V8 Javascript engine (which is also used by Chrome) with some libraries for I/O and networking.

Installation

Arch Linux:
sudo pacman -Sy nodejs
sudo pacman -Sy npm
On Windows with Chocolatey, with one of the following (I am not really sure how they differ, but see also this stackoverflow answer).
choco install -y nodejs
choco install -y nodejs-lts
choco install -y nodejs.install

APT

Distributions with APT (Debian, Ubuntu, …). npm is typically required, too:
sudo apt install nodejs
sudo apt install npm
It turns out that the installed version is quite behind (as of 2023-03-04:)
$ node -v
v12.22.12

Hello World

var http = require('http');

http.createServer(
  function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  }
).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');
Github repository about-node.js, path: /hello-world.js
The Hello World application can be started with
$ node hello-world.js

Using node.js as a shell

node.js can be used as a shell by typing the executable name node without a script name:
PS C:\Users\Rene> node
Exiting from the shell:
> process.exit()

Logging

Some basic logging functionality is provided by the logging module.
The logging module needs not be added with require('logging') (2020-09-23: this seems not to be the case anymore).
console.log('some text');
console.log('more text');
console.log('the end.' );
Github repository about-node.js, path: /api/console/log.js

Modules / require()

Types of modules

require()

Modules are imported with the special purpuse function require (which is not part of the standard JavaScript API).
In order for a module to be importable with require, the module must either be
  • a folder with a package.json file, or
  • a JavaScript file

Accessing environment variables

The values of environment variables can be accessed via process.env.ENVVAR:
console.log('Home directory: ' + process.env.HOME);
Github repository about-node.js, path: /api/process/env.js

Temporary directory

The path of the temporary directory (usually /tmp on Linux) is returned by os.tmpdir() or os.tmpdir:
const os = require('os');
console.log('The temporary directory is: ' + os.tmpdir);
Github repository about-node.js, path: /api/os/tmpdir.js

TODO

Yarn

Yarn, yet another resource navigator, is an alternative package manager to npm.
Unlike npm, Yarn does not come pre-installed with Node.js and needs therefore to be installed separately:
npm install yarn -g 

nvm

nvm is a version manager for node.js which works on POSIX compliant shells (including WSL).
nvm is installed with the following command:
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
The following command installes version 19 to ~/.nvm/versions/node/v19.…/bin/node:
nvm install 19

See also

npm is the node package manager.
~/.node_repl_history

Index