DEV Community

Sudo Space
Sudo Space

Posted on

1

Compile your NodeJS application to single file executable

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"
  }
}
Enter fullscreen mode Exit fullscreen mode

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"
    }
  }
Enter fullscreen mode Exit fullscreen mode

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...");
});
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Install npm packages :

npm i
Enter fullscreen mode Exit fullscreen mode

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

npm run build-ts
npm run build-linux-x64
Enter fullscreen mode Exit fullscreen mode

Run executable file :

./app-linux-x64
Enter fullscreen mode Exit fullscreen mode

Google AI Education track image

Build Apps with Google AI Studio 🧱

This track will guide you through Google AI Studio's new "Build apps with Gemini" feature, where you can turn a simple text prompt into a fully functional, deployed web application in minutes.

Read more →

Top comments (1)

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay