Node.js Backend to Windows EXE Compilation & Packaging Guide
lynx-anchor
This technical guide details the complete procedure for packaging the Node.js Express backend into a standalone Windows executable (server.exe). This obscures the source code, prevents end-user modification, and allows the POS system to run offline on local Windows hardware.
Table of Contents
- Why Compile to a Windows EXE?
- Project Structure Overview
- Installing Compilation Tools (
pkg) - Source Code Obfuscation Pipeline
- Compiling the Backend to
server.exe - Resolving Common Compilation & Native Module Errors
- Offline POS Desktop Deployment & Startup Script
1. Why Compile to a Windows EXE?
Packaging the Node.js backend into a native Windows executable (.exe) ensures:
- Intellectual Property Protection: Backend TypeScript and JavaScript logic is compiled into binary bytecode; raw source code is hidden.
- Tamper Resistance: Retail cashiers and store operators cannot accidentally delete or alter backend controllers, pricing calculations, or tax logic.
- Zero-Dependency Runtime: The terminal PC does not require a pre-installed Node.js runtime or
npmcommand line. - Fully Offline Operations: The local Flutter desktop application communicates directly with
localhost:3000without requiring an active internet connection.
2. Project Structure Overview
pms_backend/
├── controllers/ # Business logic controllers
├── routes/ # Express route groups
├── models/ # Sequelize database models
├── services/ # Tax, print, and AI service helpers
├── server.js # Main HTTP server entrypoint
├── package.json # Dependencies & pkg asset declarations
└── node_modules/ # Installed vendor libraries
3. Installing Compilation Tools (pkg)
Install pkg globally to compile Node.js projects into executables:
npm install -g pkg
Ensure all runtime dependencies are installed:
npm install
4. Source Code Obfuscation Pipeline (Optional but Recommended)
Before packaging into an EXE, you can obfuscate JavaScript files to transform variables and control flows into unreadable code:
npm install -g javascript-obfuscator
Execute obfuscation excluding node_modules:
javascript-obfuscator . --output dist --exclude node_modules --compact true --control-flow-flattening true
This generates the obfuscated source bundle inside dist/.
5. Compiling the Backend to server.exe
1. Configure Asset Bundling in package.json:
Because Sequelize and database drivers load files dynamically, declare them in the pkg section of package.json:
{
"name": "retail-backend",
"version": "1.0.0",
"bin": "dist/server.js",
"pkg": {
"scripts": "dist/**/*.js",
"assets": [
"node_modules/pg/**/*",
"node_modules/pg-hstore/**/*"
],
"targets": [
"node18-win-x64"
]
}
}
2. Execute the Build:
pkg dist/server.js --targets node18-win-x64 --output server.exe
This produces server.exe ready for Windows execution.
6. Resolving Common Compilation & Native Module Errors
Error 1: Native C++ Module Failure (bcrypt)
- Error:
No native build was found for platform win32 - Cause:
bcryptrelies on platform-specific C++ binaries that cannot be easily embedded into a portable Node.js executable. - Fix: Replace
bcryptwith pure JavaScriptbcryptjs:In source code:npm uninstall bcrypt
npm install bcryptjsconst bcrypt = require("bcryptjs");
Error 2: Sequelize Cannot Find PostgreSQL Driver
- Error:
Please install pg package manually - Cause: Sequelize uses dynamic
require()calls to resolve database dialect drivers, which static compilers cannot automatically trace. - Fix: Explicitly import
pgandpg-hstoreinserver.js:require("pg");
require("pg-hstore");
Error 3: Flutter Client Shows "Server Not Running"
- Error:
Connection refused: localhost:3000 - Cause: The server was bound to
127.0.0.1or blocked by Windows Defender Firewall. - Fix: Ensure the server listens on
0.0.0.0:Verify active listening port:app.listen(3000, "0.0.0.0", () => {
console.log("Server running on port 3000");
});netstat -ano | findstr :3000
7. Offline POS Desktop Deployment & Startup Script
Production Directory Layout:
FamalthLynx/
├── FamalthLynx.exe # Flutter Windows desktop UI
├── server.exe # Compiled Node.js backend
├── server_config.json # Local API connection config
└── start.bat # Automated startup script
Launcher Script (start.bat):
Create a launcher batch script to ensure the backend launches before the Flutter client:
@echo off
echo Starting Famalth Lynx Backend Service...
start /b "" server.exe
echo Waiting for backend API to initialize...
timeout /t 3 /nobreak > nul
echo Launching Famalth Lynx Application...
start "" FamalthLynx.exe
exit
Document Source: Docs/Windows-EXE-Packaging-And-Deployment-Guide.md | Verified Native Markdown for GitHub