All notes
Nodejs

Basics

To quit node.js, from SO: how to exit in node.js:

Commands

node

REPL: A Read–Eval–Print Loop (REPL), also known as an interactive toplevel or language shell, is a simple, interactive computer programming environment that takes single user inputs (i.e. single expressions), evaluates them, and returns the result to the user; a program written in a REPL environment is executed piecewise. wikipedia: REPL.

Environment variables

process

When your Node.js process boots up it will automatically provide access to all existing environment variables by creating an "env" object as property of the process global object. If you want to take a peek at the object run the the Node.js REPL with node in your command-line and type:


console.log(process.env);
console.log('The value of PORT is:', process.env.PORT);

PORTP=9999 node server.js

NODE_ENV=production node dotenv-example.js

.env

twilio.com.


// npm install dotenv --save

if (process.env.NODE_ENV !== 'production') {
  require('dotenv').load(); // This code will automatically load the .env file in the root of your project and initialize the values. It will skip any variables that already have been set.
}

inspect, debugger

Debugging

nodejs.org: debugging getting started.

When started with the "--inspect" switch, a Node.js process listens via WebSockets for diagnostic commands as defined by the Inspector Protocol, by default at host and port 127.0.0.1:9229. Each process is also assigned a unique UUID (e.g. 0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e). The full URL is ws://127.0.0.1:9229/0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e.

Inspector also includes an HTTP endpoint to serve metadata about the debuggee, including its WebSocket URL, UUID, and Chrome DevTools URL: http://localhost:9229/json/list.

Chrome DevTools 55+

Option 1: Open chrome://inspect in a Chromium-based browser. Click the Configure button and ensure your target host and port are listed.
Option 2: Copy the devtoolsFrontendUrl from the output of /json/list (see above) or the --inspect hint text and paste into Chrome.
Option 3: Install the Chrome Extension NIM (Node Inspector Manager)

Commands

nodejs.org: debugger.



# Yes, just use the name "myscript.js"
node inspect myscript.js
< Debugger listening on ws://127.0.0.1:9229/80e7a814-7cd3-49fb-921a-2e02228cd5ba
< For help see https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in myscript.js:1
> 1 (function (exports, require, module, __filename, __dirname) { global.x = 5;
  2 setTimeout(() => {
  3   console.log('world');
debug>

# Or
node-inspector myscript.js