Lädt...


🔧 Compile your NodeJS application to single file executable


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Hi great developers :)
In this article, I am trying to share with you my small experience about converting nodeJS projects to single file executable.
In short, there are methods that you can use.

  1. NodeJS single file executable built-in feature
    Here I link NodeJS documentation. Because it is straight. But it is still an experimental feature and may have some issues. My problem with it was that when I compiled my program in Linux, it showed me the message Segmentation fault (core dumped). I tested the same steps on Windows and there was no problem with the implementation and it worked well.

  2. Bun
    You can use Bun because it supports compilation to single file executable and it is very easy to work with. But all npm packages developed for NodeJS may not work well on it. If your program works well on Bun, you can use Bun for this. The problem I had with Bun was that it couldn't work properly with node:fs.WriteStream.

  3. Deno
    I have not tried Deno. But you can read its documentation. Of course, I don't think Deno is fully compatible with nodeJS packages. (as I understood from its documentation)

  4. The method I used and it worked
    I used pkg. Of course, I don't mean vercel pkg because its development has stopped, but we can use from yao-pkg. It's an active fork of vercel pkg that also supports node20.

Let's implement an example together:

Make a folder and create a file as package.json in that :

{
  "name": "test-bin",
  "version": "1.0.0",
  "main": "app.js",
  "scripts": {
    "build-ts": "tsc",
    "build-linux-x64": "pkg --targets node20-linux-x64 dist/app.js -o app-linux-x64"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "devDependencies": {
    "@types/express": "^4.17.21",
    "@types/node": "^20.14.2",
    "@yao-pkg/pkg": "^5.11.5",
    "typescript": "^5.4.5"
  },
  "dependencies": {
    "express": "^4.19.2"
  }
}

make a file as tsconfig.json with this content:

{
    "compilerOptions": {
      "target": "es2022",
      "lib": ["es2022"],
      "module": "commonjs",
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
      "strict": true,
      "skipLibCheck": true,
      "rootDir": "./src",
      "outDir": "./dist"
    }
  }

Make src folder and create file as app.ts in that :

import express from "express";

import router from "./routes/router";

const app = express();

app.use(express.urlencoded({ extended: true }));
app.use(router);

app.listen(3000, () => {
  console.log("SERVER IS RUNNING...");
});

Make a folder as routes in src and create a file as router.ts in that:

import { Router } from "express";

const router = Router();

router.get("/", (req, res) => {
  res.status(200).json({ message: "I work fine :D" });
});

export default router;

Install npm packages :

npm i

Run these commands to compile your project to single file executable :

npm run build-ts
npm run build-linux-x64

Run executable file :

./app-linux-x64
...

🔧 Compile your NodeJS application to single file executable


📈 71.24 Punkte
🔧 Programmierung

🐧 Compile Python code into an executable Linux application


📈 40.67 Punkte
🐧 Linux Tipps

🕵️ Medium CVE-2020-7635: Compass-compile project Compass-compile


📈 37.08 Punkte
🕵️ Sicherheitslücken

🐧 Any idea how to compile geogebra into an executable or .deb?


📈 34.69 Punkte
🐧 Linux Tipps

📰 Codecov Changes the Bash Uploader for a NodeJS executable


📈 29.54 Punkte
📰 IT Security Nachrichten

📰 Codecov ditches Bash Uploader for a NodeJS executable


📈 29.54 Punkte
📰 IT Security Nachrichten

🔧 Aligning NodeJS with the Web: Should NodeJS Implement The Same APIs as the Web Browser?


📈 26.78 Punkte
🔧 Programmierung

🐧 Web cam white/black board in single executable with all dependencies (experimental)


📈 25.89 Punkte
🐧 Linux Tipps

🐧 monolinux - An embedded Linux distro with a single statically linked executable.


📈 25.89 Punkte
🐧 Linux Tipps

🔧 Making a tiny .NET Core 3.0 entirely self-contained single executable


📈 25.89 Punkte
🔧 Programmierung

🐧 Easy to use non-root container packed into a single executable


📈 25.89 Punkte
🐧 Linux Tipps

🐧 I made a CLI tool to wrap any website into a single executable AppImage.


📈 25.89 Punkte
🐧 Linux Tipps

🐧 Can't get an application to compile


📈 24.52 Punkte
🐧 Linux Tipps

🔧 How nodeJS handle multiple task being single-threaded


📈 23.14 Punkte
🔧 Programmierung

🕵️ CVE-2022-45145 | CHICKEN up to 5.3.0 EGG File egg-compile.scm os command injection


📈 22.64 Punkte
🕵️ Sicherheitslücken

📰 Java-Stager - A PoC Java Stager Which Can Download, Compile, And Execute A Java File In Memory


📈 22.64 Punkte
📰 IT Security Nachrichten

🪟 Fix: Invalid Game Executable in Fortnite [Application Error]


📈 22.13 Punkte
🪟 Windows Tipps

🐧 How to compile your own kernel for Debian/Ubuntu – without paying $20 and violating GPL


📈 21.87 Punkte
🐧 Linux Tipps

🎥 Clone, Compile & Branch Your Code with Git [Tutorial]


📈 21.87 Punkte
🎥 IT Security Video

🕵️ WinSparkle bis 0.5.2 Executable File Loading erweiterte Rechte


📈 20.25 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2024-43199 | Nagios NDOUtils up to 2.1.3 Executable File permission assignment


📈 20.25 Punkte
🕵️ Sicherheitslücken

🕵️ Liebert MultiLink Automated Shutdown 4.2.4 LiebertM Executable File erweiterte Rechte


📈 20.25 Punkte
🕵️ Sicherheitslücken

🕵️ Can somebody help me get a DLL from a executable file?


📈 20.25 Punkte
🕵️ Reverse Engineering

🕵️ CVE-2020-24307 | mRemoteNG 1.76.20 Executable File Privilege Escalation (ID 170794)


📈 20.25 Punkte
🕵️ Sicherheitslücken

matomo