LinuxCommandLibrary

node

Run JavaScript code using Node.js

TLDR

Run a JavaScript file

$ node [path/to/file]
copy

Start a REPL (interactive shell)
$ node
copy

Execute the specified file restarting the process when an imported file is changed (requires Node.js version 18.11+)
$ node --watch [path/to/file]
copy

Evaluate JavaScript code by passing it as an argument
$ node [[-e|--eval]] "[code]"
copy

Evaluate and print the result, useful to print node's dependencies versions
$ node [[-p|--print]] "process.versions"
copy

Activate inspector, pausing execution until a debugger is connected once source code is fully parsed
$ node --no-lazy --inspect-brk [path/to/file]
copy

SYNOPSIS

node [options] [script.js | -e "script_string" | - ] [script_arguments...]
node [options] (starts REPL if no script or eval provided)

PARAMETERS

-v, --version
    Prints the Node.js version.

-h, --help
    Displays Node.js command-line options and usage information.

-e "script", --eval="script"
    Executes the provided JavaScript script string as code.

-p "script", --print="script"
    Executes the provided JavaScript script string and prints the result to stdout.

-c, --check
    Performs a syntax check on the script without executing it.

-i, --interactive
    Enters the Node.js REPL (Read-Eval-Print Loop) after the script finishes execution.

-r "module", --require="module"
    Preloads the specified module at startup, useful for polyfills or debuggers.

--inspect[=[host:]port]
    Activates the Node.js inspector agent for debugging and profiling, listening on the specified host and port (default: 127.0.0.1:9229).

--experimental-modules
    Enables experimental ECMAScript module (ESM) support.

--no-warnings
    Suppresses all Node.js process warnings.

--trace-warnings
    Prints stack traces for Node.js process warnings.

--v8-options
    Prints command-line options for V8, Node.js's JavaScript engine.

DESCRIPTION

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside a web browser. It leverages Google's V8 JavaScript engine, known for its high performance. Node.js empowers developers to build scalable network applications, commonly employed for web servers, APIs, and microservices.

Its unique event-driven, non-blocking I/O model makes it remarkably lightweight and efficient, particularly suited for data-intensive real-time applications. Node.js is complemented by a vast ecosystem of open-source libraries, primarily managed by npm (Node Package Manager). It provides robust support for common server-side operations, including file system access, network communication, and database interaction, making it a versatile platform for both backend development and command-line tools.

CAVEATS

While powerful, Node.js's single-threaded event loop can become a bottleneck for CPU-bound tasks if not properly managed (e.g., by offloading to worker threads). Memory management requires careful attention in large-scale applications to prevent leaks. Compatibility issues with specific npm packages and Node.js versions can also arise, necessitating careful dependency management.

NPM (NODE PACKAGE MANAGER)

npm is the default package manager for Node.js, providing access to a vast registry of open-source JavaScript libraries and tools. It simplifies dependency management, package installation, and project scaffolding for Node.js applications.

REPL (READ-EVAL-PRINT LOOP)

Node.js includes an interactive REPL environment accessible by running `node` without arguments. This console allows users to execute JavaScript code snippets, test functionalities, and experiment with Node.js APIs directly, making it an excellent tool for learning and debugging.

EVENT LOOP

At its core, Node.js operates on an event loop, which allows it to perform non-blocking I/O operations despite being single-threaded. This mechanism efficiently handles asynchronous tasks (like network requests or file operations) by offloading them and continuing with other code, then processing their results when they complete, leading to high throughput.

HISTORY

Created by Ryan Dahl in 2009, Node.js emerged as a groundbreaking platform for server-side JavaScript, utilizing Google's V8 engine. Its innovative event-driven, non-blocking I/O model quickly gained traction for building scalable network applications. Initially Linux-centric, its support rapidly expanded to other operating systems. A significant event in its history was the 2014 fork into io.js due to governance concerns, which subsequently merged back into the Node.js project in 2015, strengthening its community and development process. Node.js continues to be a cornerstone of modern web development, maintained by the OpenJS Foundation.

SEE ALSO

npm(1), nvm(1), yarn(1), python(1), perl(1), ruby(1)

Copied to clipboard