---
title: Application structure and modes
project: Total.js V4
version: 0.0.99
section: guides
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/guides/application-structure/
updated: 2026-11-16T15:41:15.818Z
---

# Application structure and modes

> Which directories Total.js 4 loads and in which order, debug and release modes, bundles, packages and workers.

A Total.js 4 application is a directory. You do not register files: the framework reads well-known directories when it starts, `require()`s every script it finds and calls its `exports.install()`. This page explains which directories are read and in which order, how the debug and release modes differ, how bundles and packages work, and how to test the application.

## Starting the application

The start script (usually `index.js`) loads `total4/debug` or `total4/release` and passes an options object. Both modules export a function:

```js
var options = {};
// options.port = 8000;
// options.ip = '127.0.0.1';
// options.watch = ['adminer'];
// options.livereload = true;
require('total4/debug')(options);
```

For production, use `require('total4/release')(options)`. A free command-line argument is read as the port (`node index.js 8001`), as `http://ip:port`, or otherwise as the path of a Unix socket.

| | `total4/debug` | `total4/release` |
| --- | --- | --- |
| `DEBUG` / `RELEASE` | `true` / `false` | `false` / `true` |
| Process | A watcher process forks the app | The app runs in the same process, unless `--watcher` or `options.watcher` is set |
| Restart | On every change in the watched directories | Only when the app exits or asks for a restart |
| Files `*-debug.js` | Loaded | Skipped |
| Files `*-release.js` | Skipped | Loaded |
| Source maps | Generated (unless `CONF.nosourcemap`) | Not generated |

Both modes write a `.pid` file named like the start script (`index.pid`). Delete it and the process ends within about four seconds. That is a simple way for a deployment script to stop the app.

## What the watcher watches

In debug mode the watcher looks every few seconds at `.js`, `.ts`, `.resource`, `.package`, `.bundle`, `.build`, `.flow` and `.url` files, at `jsonschemas/*.json` and at the configuration files in the root (`config`, `config-debug`, `config-release`, `versions`, `sitemap`, `bundles.debug`). It reads the directories `components`, `controllers`, `definitions`, `operations`, `extensions`, `modules`, `models`, `builds`, `jsonschemas`, `schemas`, `actions`, `tasks`, `resources`, `source`, `workers`, `packages`, `themes`, `middleware`, `configs`, `bundles`, `flowstreams`, `startup`, `threads` and `plugins`, plus the directories in `options.watch`. Views and public files do not cause a restart. With `options.livereload` the watcher also watches them and tells the browser to reload.

Each change prints a line (`ADD`, `UPD`, `REM`) and restarts the app. The event `ON('watcher', process)` runs in the watcher process; `ON('debug_start')` and `ON('debug_restart')` run in the app (see [Scheduling and events](../scheduling-and-events/)).

## The load order

When the server starts, the framework reads the directories below in this order. Every directory has a `CONF.directory_*` key (see [Configuration](../configuration/)), and scripts in subdirectories are loaded too.

| # | Directory | What is loaded |
| --- | --- | --- |
| 1 | `/jsonschemas/` | `*.json` and `*.txt` JSON schemas |
| 2 | `/modules/` | Modules, available through `MODULE()` and `MODS` |
| 3 | `/packages/` | `.package` files or directories |
| 4 | `/models/` | Models, available through `MODEL()` |
| 5 | `/schemas/` | Schemas (see [Schemas](../schemas/)) |
| 6 | `/actions/` | Actions |
| 7 | `/tasks/` | Tasks |
| 8 | `/operations/` | Operations (see [Operations and tasks](../operations-and-tasks/)) |
| 9 | `/themes/` | Each subdirectory is a theme, its `index.js` is installed |
| 10 | `/definitions/` | Definitions: global code, delegates, `AUTH()` |
| 11 | `/controllers/` | Controllers with routes |
| 12 | `/components/` | `.html` components |
| 13 | `/middleware/` | Middleware |
| 14 | `/builds/` | `.build` and `.url` files of UI Builder |
| 15 | `/extensions/` | Extensions, through `NEWEXTENSION()` |
| 16 | `/plugins/` | `plugins/{name}/index.js` and `plugins/{name}.html` |
| 17 | `/flowstreams/` | `.flow` files (see [FlowStream](../flowstream/)) |

The order is the order of installation, not of `require()` only. Everything you declare in a definition already sees the models, schemas and operations. When all files are installed, the framework sorts the routes, loads the preferences and emits `load` and `ready`.

A plugin is a small application inside `plugins/{name}/`. Besides its `index.js`, the framework loads its own `definitions`, `operations`, `actions`, `schemas`, `tasks` and `flowstreams` directories.

Some files are always skipped: names that start with a dot, and names that end with `-bk.js` or `_bk.js`. Use these suffixes to keep an old copy next to the new one.

## exports.install

Each file can export `install(options)`. The framework calls it once, right after `require()`. The argument is the configuration key `{type}_{name}`, for example `module_mail` for `/modules/mail.js`:

```js
// /modules/mail.js
exports.install = function(options) {
	// options === CONF.module_mail
};

exports.send = function(to, subject, body) {
	// ...
};
```

After installation the framework emits `ON('install', type, name, module)`.

## Using installed code

```js
// A module
MODULE('mail').send('info@example.com', 'Hello', 'Body');
MODS.mail.send('info@example.com', 'Hello', 'Body');

// A model
var Product = MODEL('product');

// A plugin
PLUGINS.dashboard.id; // 'dashboard'

// A source file (/source/helpers.js), loaded the first time you ask for it
var helpers = INCLUDE('helpers');

// Any script relative to the application directory
var tools = REQUIRE('lib/tools.js');
```

## Bundles

A bundle is one `.bundle` file that contains a whole application or a part of it. Put bundles into `/bundles/`. On start the framework extracts every bundle into the hidden directory `/.src/`, then copies the files of the application over it, and runs the application from `/.src/`.

- Files of the application win over files of the bundle, so you can override a single view or a script.
- `config`, `config-debug`, `config-release`, `sitemap`, `versions`, `workflows`, `dependencies` and `.resource` files are merged, not replaced.
- A file whose name starts with `--` is appended to the file without the prefix: `/public/css/--custom.css` is appended to `/public/css/custom.css`.
- Existing files in `/databases/` and `/updates/` are never overwritten.
- A `.url` file in `/bundles/` contains a URL; the bundle is downloaded from it.
- `.bundleignore` in the root lists paths that are not copied.

Create a bundle with the command line: `total4 bundle app.bundle`. Extract one with `total4 extract app.bundle`.

## Packages

A package is a `.package` file (or a directory named `name.package`) in `/packages/`. It is extracted into the temporary directory and its `index.js` is installed. Read its files with `PATH.package('name/file.txt')`.

## Loading without a server

`LOAD()` installs the application without starting the HTTP server. Use it in scripts, workers and tests:

```js
require('total4');

LOAD('config,definitions,models,schemas,operations', function() {
	// the code is installed
});
```

## Workers

`/workers/` contains scripts that run in a separate process (`WORKER()`, `NEWFORK()`) or a worker thread (`NEWTHREAD()`). They are not installed on start.
