Skip to content

Instantly share code, notes, and snippets.

@nath1as
Last active November 9, 2021 16:32
Show Gist options
  • Save nath1as/a30634026146e805fb5252e7a8f195d3 to your computer and use it in GitHub Desktop.
Save nath1as/a30634026146e805fb5252e7a8f195d3 to your computer and use it in GitHub Desktop.
node CLI overview
# Node CLI
We can just run node as a command to enter REPL, we can run js files, evaluate js strings with the option -e
## Execute
node [options] [V8 options] [script.js | -e "script" | -] [--] [arguments]
We can run a file with node, provide node with -e flag and a string of code to evaluate, or provide code wit hstdin. We can provide multitude of options to node (man node), and arguments to our programs.
## REPL
Running node enters Read-Eval-Print-Loop
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the repl
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
for fancy completion syntax and colors use jay
## Debug
node inspect [script.js | -e "script" | <host>:<port>] …
When started with the --inspect flag, a Node.js process listens for a debugging client. By default, it will listen at host and port 127.0.0.1:9229. Each process is also assigned a unique UUID.
Debug syntax:
- setBreakpoint(10) : Set a break point in line 10.
- next : Execute to next js statement.
- cont : Continue to execute until stop at next break point or application exit.
- step : Step into current executing function if current statement is a function.
- out : Step out current executing function.
- backtrace : Display current executing stack trace.
- repl : Start node REPL ( Read Eval Print Loop ) to inspect variable value and execute code.
- watch(expr) : Add expression to watch list, so the expression value will be displayed when code executing.
- list(n) :List the N line in front and back of the current stop line in the debugger.
## Options
man node provides us with the full list of node, enviroment and v8 options. Some of the common options are:
-e string
Evaluate string as JavaScript.
-p string
Identical to -e, but prints the result.
-h
Print command-line options.
-i
Open the REPL even if stdin does not appear to be a terminal.
-r module
Preload the specified module at startup.Follows `require()`'s module resolution rules. module may be either a path to a file, or a Node.js module name.
-v
Print node version.
-c
Check the script's syntax without executing it. Exits with an error code if script is invalid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment