Node API and lessc
The Node runtime gives Less two ways to compile stylesheets: a programmatic API for other Node tools and lessc for command-line use. Both routes use the same compiler core. The Node layer supplies access to local files, remote URLs, plugins, image dimensions, and the operating system.
Node runtime
The Node runtime adapts the compiler to services that can read files, resolve modules, and write output. It also exposes the main API objects so a build tool can control compilation without starting lessc.
Node entry point
The Node entry point is home/…/index.js. It creates a Less API with local and URL file access, then adds Node-specific helpers to that API. This makes less.render and less.parse the main programmatic entry points while keeping the compiler core independent of Node.
The entry point also exposes the file manager classes, the plugin loader, the file-system adapter, and a command-line helper. A caller that needs a different host can use createFromEnvironment and provide its own environment and file managers. See runtime integrations for the wider host boundary and browser runtime for the browser counterpart.
References: packages/…/index.js• L1-31 packages/…/index.js• L23-58 packages/…/package.json• L25-41
File access
The file access layer turns an import path into stylesheet content and a resolved name. The environment asks registered file managers in reverse order, so a plugin can add a more specific manager while the Node runtime supplies the default local and URL managers.
The local file manager searches the current file directory, configured include paths, and the working directory. It supports both asynchronous and synchronous reads. It can also resolve package-style imports and try the expected file extension. This gives Less imports a consistent search model for local files and installed packages. See imports and file resolution for import behavior in the compiler.
References: packages/…/environment.js• L8-56 packages/…/file-manager.js• L8-18 packages/…/file-manager.js• L31-53 packages/…/file-manager.js• L84-153
URL file access
The URL file manager handles imports whose path or current directory uses HTTP or HTTPS. It fetches the resource, follows a limited number of redirects, and returns the response as stylesheet content. This lets a Node compilation use remote stylesheets through the same import system as local files.
Remote access is optional. If the HTTP client is not available, the import reports a file error instead of silently changing the source. HTTP errors and empty responses remain visible through compiler errors or warnings.
References: packages/…/url-file-manager.js• L10-20 packages/…/url-file-manager.js• L22-57 packages/…/environment.js• L28-47
Plugin loading
Plugins extend compilation with features such as functions, visitors, processors, and file managers. The Node loader finds a plugin from a relative path, a module name, or the less-plugin- naming convention, then passes it into the shared plugin life cycle. See plugin architecture for the extension model.
lessc loads command-line plugins before it starts rendering. Less can also load a plugin from an @plugin import during parsing unless the command disables that rule. A plugin can receive options and can be checked against the running Less version before it becomes active.
References: packages/…/plugin-loader.js• L10-59 packages/…/abstract-plugin-loader.js• L12-115 packages/…/parse.js• L30-83 packages/…/plugin-manager.js• L4-15
Image support
The Node runtime adds image-size, image-width, and image-height as Less functions. These functions resolve an image through the active file-manager system, inspect its bytes, and return pixel dimensions that Less can use in an expression.
Image probing is an optional Node capability. If the image probe package is absent, the functions return zero dimensions. An unreadable or unknown image produces a file error so a build does not use a false size without notice.
References: packages/…/index.js• L25-30 packages/…/image-size.js• L9-57 packages/…/image-size.js• L59-78
lessc command-line compiler
lessc is the command-line front end for the Node API. It converts command-line arguments into compiler options, prepares plugins, reads the source, and sends the result to a file or standard output.
Command-line contract
The command accepts a source and an optional destination. A source of - means that Less reads from standard input. A destination sends CSS to a file; without one, CSS goes to standard output. The front end resolves file paths from the current working directory before it starts compilation.
The command also reports version and help information without compiling. It validates required option values and sets a failing process status when input, plugin, source-map, read, write, or compile work cannot complete.
References: packages/…/lessc• L69-74 packages/…/lessc• L76-151 packages/…/lessc• L327-339 packages/…/lessc-helper.js• L21-40
Option translation
The argument parser uses the public command names as a thin mapping to compiler options. The important groups are:
| Command-line concern | Compiler effect |
|---|---|
| Include paths | Adds search locations for imports through paths. |
| Variable injection | Adds values before or after the source through globalVars and modifyVars. |
| URL behavior | Controls root paths, URL rewriting, and URL query arguments. |
| Evaluation rules | Selects math mode, strict units, inline JavaScript, and compression. |
| Diagnostics | Selects lint mode, dependency output, warning control, and error output. |
| Source maps | Enables a map and supplies its path, URL, source contents, and annotation rules. |
| Plugins | Queues a plugin name and its options, or uses a plugin shorthand. |
Short forms map to the same options as their long forms. Deprecated names remain accepted where the command can translate them, and the front end emits a warning instead of treating them as new behavior.
References: packages/…/lessc• L373-400 packages/…/lessc• L403-467 packages/…/lessc• L469-569 packages/…/lessc• L574-661 packages/…/default-options.js• L2-88
Compilation and output flow
The command follows one path from argument state to compiler result. This diagram shows how command options join the Node API and how the result reaches the user.
flowchart LR
A[CLI arguments] --> B[Compiler options]
B --> C[Load queued plugins]
C --> D[Read source or stdin]
D --> E[less.render]
E --> F[CSS and imports]
F --> G[CSS file or stdout]
F --> H[Source map when enabled]
F --> I[Dependency list when requested]
Before rendering, lessc adds the source directory to the import search paths and records the source filename. Lint mode stops after parsing and validation. Normal mode writes CSS, then writes an external source map when requested, or reports the imported files when dependency mode is active. See compiler pipeline for the stages inside less.render.
References: packages/…/lessc• L268-325 packages/…/lessc• L342-369 packages/…/lessc• L183-265 packages/…/render.js• L3-37
Source-map handling
Source-map options connect compiler metadata to command output. The command can derive a map name from the CSS destination, accept an explicit map file, embed the map in CSS, include Less source text, or set the URL used by the CSS annotation.
The command rejects combinations that cannot describe one clear map, such as a custom map URL with a request to remove the map annotation. It creates parent directories for CSS and map files before writing them. See compiler pipeline for the map’s role after evaluation.
References: packages/…/lessc• L100-143 packages/…/lessc• L183-221 packages/…/lessc• L223-253 packages/…/lessc• L490-533 packages/…/parse-tree.js• L36-118
Host and extension boundaries
The Node layer owns host services while the compiler owns parsing, evaluation, and CSS generation. This boundary allows the same Less language to run in Node, in a browser, or in another host that supplies equivalent environment services.
Environment services
The Node environment supplies base64 encoding, MIME lookup, character-set lookup, and source-map generation. Optional packages provide these services when present, with safe fallbacks for missing MIME or source-map support. File access stays separate from these helpers so a host can replace one capability without replacing the others.
References: packages/…/environment.js• L5-43 packages/…/index.js• L23-33
Extension points
The compiler asks the environment for a matching file manager for each import. The active plugin manager can add file managers to that choice and can also add work before or after the core compilation stages. This is the main relationship between host services and plugins: the host supplies access, while plugins can extend how that access or processing behaves.
References: packages/…/environment.js• L28-56 packages/…/plugin-manager.js• L34-41 packages/…/plugin-manager.js• L66-97 packages/…/parse-tree.js• L103-118
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the Node entry point’s exposed API aligned with the package
import,require, and browser export paths. - Keep file-manager selection compatible with both normal imports and synchronous consumers such as image dimension functions.
- Keep plugin name resolution, command-line plugin queuing, and
@pluginloading aligned so a plugin behaves the same at each entry point. - Keep source-map path derivation aligned between
lesscoutput paths and the compiler’s source-map metadata. - Keep option aliases and deprecation behavior aligned with the help text so users do not receive a valid-looking command with a different effect.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The source path is missing. | lessc exits with an error and prints usage information. | Supply a source path or use - for standard input. |
| A requested plugin cannot be found or loaded. | Compilation does not start and the process exits with a plugin error. | Install the plugin where Node can resolve it and use its supported name or path. |
| A remote import needs HTTP support but the optional client is absent. | The import fails with a file-manager error. | Install the optional HTTP client or use a local import. |
| An external source map is requested without a CSS output path. | lessc rejects the map request unless the map is embedded. | Provide a destination or use inline source-map output. |
| Image probing is unavailable or the image format is unknown. | Image functions return zero dimensions in the first case or fail in the second. | Install image probing support and use a recognized, readable image. |
| A source-map URL is combined with disabled source-map annotation. | lessc rejects the conflicting request. | Choose a custom URL or disable the annotation, not both. |
Key files
| File | Why to read it |
|---|---|
| packages/…/index.js | Defines the Node API surface and connects Node services to the compiler. |
packages/less/bin/lessc | Shows the command-line contract, option translation, plugin setup, and output flow. |
| packages/…/file-manager.js | Explains how local files and package-style imports become compiler input. |
| packages/…/url-file-manager.js | Explains how HTTP and HTTPS imports enter the same file-loading path. |
| packages/…/plugin-loader.js | Shows how Node finds plugins before the shared plugin life cycle runs. |
| packages/…/image-size.js | Defines the Node-only image dimension functions. |
| packages/…/environment.js | Defines the host boundary for file-manager selection. |
| packages/…/parse.js | Shows how a render session receives options and plugins. |
| packages/…/parse-tree.js | Shows how compiler results, source maps, post-processors, and imports become output. |
| packages/…/package.json | Defines the package entry points, lessc command, and optional Node capabilities. |