Skip to content

Lakutata Packaging ​

A compiled application can be shipped without its node_modules, in three forms:

  • a bundle: one JavaScript file that Node.js runs;
  • an executable: one program that runs without Node.js installed;
  • installers: system packages (deb, rpm, pkg, msi…) that install the executable, put its program in the PATH and can register it as a service (Installers).

lakutata package bundle, package exe and package installer build them with @lakutata/builder, which the project installs (npm install --save-dev @lakutata/builder; templates that pack their application already include it).

A mobile application created from the mobile-react template is shipped differently: lakutata mobile stage bundles its application for the Node.js that the mobile application embeds, and its native projects produce the release builds for the stores (see the section "The packaging" in the README that the template provides for the project).

1. What is packed ​

  • The compiled application: the output of tsc (which emits decorator metadata), not the TypeScript sources, so build it first. The entry is --entry, otherwise the main field of the package.json, otherwise build/App.js.
  • Its dependencies, bundled by esbuild into one file, except native addons and files read by path (sections 3 and 4). The framework's packages, and those built on it, are always bundled, so that there is one copy of the framework, its classes and its container.
  • CommonJS: the bundle is a CommonJS file, so the application cannot use top-level await; await inside an async function instead.
  • Optional dependencies that are not installed (drivers for the stores the application does not use) are left out: loading one throws at run time, as it would without the bundle.
  • Resources: the other files the application reads at run time (a web interface's built pages, templates, data) are declared in its package.json (section 6).

2. Bundles ​

bash
npm run build
npx lakutata package bundle      # dist/<name>.cjs
node dist/greet.cjs
✔ dist/greet.cjs (1.1 MB): node dist/greet.cjs
✔ dist/greet.native: the native addons (better-sqlite3) and the scripts run on their own (build/child.js), distributed with the bundle
✔ dist/greet.resources: the resources (texts), distributed with the bundle

The name is the first bin entry of the package.json, otherwise the package name without its scope. When needed, two directories are written next to the bundle and must be shipped with it: dist/<name>.native/ (native addons, scripts run on their own, files read by path) and dist/<name>.resources/ (the resources). Once moved together with them, the bundle runs from any directory.

OptionMeaning
--entry <file>The compiled entry
--outfile <file>The bundle, dist/<name>.cjs by default
--no-minifyDo not minify the bundle (class names are kept either way)
--external <modules...>Modules left out of the bundle, loaded at run time from a node_modules next to it
--node <version>The Node.js version that runs the bundle (18.10 for NW.js 0.70): the bundle is written in the syntax that this version reads; by default, the version running the tool

3. Native addons ​

Packages holding .node files (better-sqlite3, the workspace addons made by lakutata add --native), and packages that load them through computed paths (sharp), cannot be bundled. They are left out and laid out as in node_modules in dist/<name>.native/, with what they load at run time:

  • all their files, and those of the packages that provide the shared libraries their addons link (such as sharp's libvips), but not their C and C++ sources and objects (better-sqlite3 holds the SQLite sources), nor what built them (the node-gyp and CMake files in build/, cargo's target directory, the static and import libraries);
  • the JavaScript they load;
  • the application's own addons (createRequire(import.meta.url)('./x.node')).

The packages' type declarations, source maps, TypeScript sources and documentation are left out. An addon is built for the machine's platform and architecture, and needs the system libraries it was built for, so pack the application for each platform on that platform.

4. Files read by path ​

The bundle is not where the modules were: a script started on its own (a worker, a child process started by fork()) or a file the application reads (defaults.json, a schema directory) would not be found next to it. The tool finds paths built from a module's location, in the application's modules and in those of its packages, and rewrites them to point into dist/<name>.native/:

Path
new URL('./worker.js', import.meta.url)Also pathToFileURL(__filename)
path.join(import.meta.dirname, 'child.js'), path.resolve(__dirname, '../schema')Also with path.dirname(fileURLToPath(import.meta.url))
__dirname + '/worker.js', `${__dirname}/worker.js`
require.resolve('./worker')The extension is resolved as Node.js resolves it
path.join(__dirname, 'locales', `${lang}.json`)Partly known: every file its pattern matches is shipped
The path names
A script (.js, .cjs)Bundled separately, with what it loads
Another file, a data directoryCopied
A directory that holds the module (path.join(__dirname, '..'): the application, its package)Not shipped, with a warning: declare its files as resources
A directory that holds scripts or packages (package.json, node_modules)Not shipped, with a warning: its scripts would need their packages at run time
Nothing yet (a directory the application creates)Left unchanged
typescript
import {fork} from 'node:child_process'
import {readFileSync} from 'node:fs'
import path from 'node:path'

//Both are shipped: the script bundled on its own, the file copied
fork(path.join(import.meta.dirname, 'child.js'), ['one', 'two'])
const defaults: Record<string, unknown> = JSON.parse(readFileSync(new URL('./defaults.json', import.meta.url), 'utf8'))

The tool warns, with the file and line, about what it cannot follow in the application's modules: a worker whose path is known only at run time (new Worker(file)) or relative to the current directory (new Worker('./worker.js')), and a script that the application also imports (it would be bundled twice, with two copies of its module). Build these paths from their module's directory, or ship the files as resources.

5. Executables ​

bash
npx lakutata package exe                   # dist/<name> (dist/<name>.exe on Windows), with Node.js
npx lakutata package exe --runtime bun     # with Bun
› Node.js v24.11.1 embedded
› Resources embedded, extracted at the first run: texts
› Scripts run on their own embedded, extracted at the first run: build/child.js
✔ dist/greet (darwin-arm64, 112.9 MB)

An executable is built for the platform it is built on. Build executables for other platforms on those platforms, because their native addons are built there: a CI matrix of Linux, macOS and Windows machines, or, for Linux, a container of each architecture (docker run --platform linux/arm64); the cli template's README shows both.

OptionMeaning
--runtime node (default)The bundle is embedded in a copy of the Node.js that runs the tool, or of --node-program (a Node.js single executable application): about 110 MB on macOS arm64
--runtime bunThe bundle is compiled by Bun (the project's bun package, otherwise the system's bun): about 70 MB on macOS arm64, and it starts faster; the runtime is then Bun
--layout file (default)One file: the native addons, the files read by path and the resources are embedded, and extracted when it starts
--layout directorydist/<name>/ holds the executable, native/ and resources/, loaded from there without extraction (the installers use it)
--outdir <directory>Where the executable is written, dist by default
--node-program <path>The Node.js program to embed instead of the one running the tool: Node.js 20.12, 21.7 or later ($(nvm which 22)), so that an older Node.js can run the tool; the bundle follows its syntax
--entry, --no-minify, --externalAs for a bundle
  • Extraction: a file-layout executable extracts what it embeds on its first run into its cache directory, in a directory named after the content, so each version extracts once. The cache directory is ~/Library/Caches/<name> on macOS, %LOCALAPPDATA%\<name>\Cache on Windows, $XDG_CACHE_HOME/<name> or ~/.cache/<name> on Linux, or the directory specified by LAKUTATA_NATIVE_DIR. When the cache directory is not writable, it uses <name> in the temporary directory (unless LAKUTATA_NATIVE_DIR is set).
  • Native addons: Bun loads Node-API addons, but may not load those built on the Node.js V8 API; an executable built with Bun warns about them, and fails when it loads such a package (better-sqlite3, for example). On Alpine (musl), executables built with Node.js do not load native addons; build the executables with Bun (--runtime bun).
  • External modules: the --external modules are loaded from a node_modules next to the executable.
  • Child processes: an executable started for one of its scripts (fork(path.join(import.meta.dirname, 'child.js'))) runs that script, in both layouts, with its arguments. With Node.js, require.main is that script, as in Node.js; with Bun, it is not.
  • No .env: executables do not load the .env file in the directory they run in; use LAKUTATA_ENV_FILE (section 6).
  • Icon and signature: lakutata.icon in the package.json (a square PNG of 256 px or more) becomes the icon of Windows executables. macOS executables are signed ad hoc. Windows executables built with Node.js have no valid signature (the signature of node.exe no longer matches, and setting lakutata.icon removes it): sign them (signtool) before distributing them.

6. At run time ​

Resources are declared as a list of {from, to} objects in lakutata.resources: from is a file or directory in the project (it may be outside the package, such as ../ui/dist), and to is its path among the resources.

json
{
  "lakutata": {
    "resources": [{"from": "../ui/dist", "to": "ui"}]
  }
}

isPackaged() and resourcesPath() (from lakutata) tell the application how it runs:

How it runsisPackaged()resourcesPath()
Not packed (node build/App.js, electron .)falseundefined
A bundletruedist/<name>.resources next to the bundle
An executable, file layouttrueThe directory the resources were extracted to
An executable, directory layout, or an installed applicationtrueresources next to the executable
A packaged Electron applicationtruemain.resources in Electron's resources
An NW.js application (run by lakutata nwjs run, or packaged)trueresources in the application directory

resourcesPath() returns the path even when the application declares no resources, so the directory may not exist. The application chooses where to read its files from:

typescript
import {createRequire} from 'node:module'
import path from 'node:path'
import {resourcesPath} from 'lakutata'

const resources: string | undefined = resourcesPath()
//The built pages of the "ui" package of the workspace: among the resources when packed, in its dist directory otherwise
const ui: string = resources ? path.join(resources, 'ui') : path.join(path.dirname(createRequire(import.meta.url).resolve('ui/package.json')), 'dist')

Every packed form loads the environment file named by LAKUTATA_ENV_FILE before the application launches. The file holds NAME=value or NAME="value" lines, and comments (#). Its values replace variables that are already set.

bash
LAKUTATA_ENV_FILE=/etc/greet.env ./greet

Released under the MIT License.