Compile your NodeJS application to single file executable
Tags: typescript, javascript, node
Published: 2024-06-09 13:59:07

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](https://nodejs.org/api/single-executable-applications.html). 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](https://bun.sh/docs/bundler/executables) 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](https://docs.deno.com/runtime/manual/tools/compiler). 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](https://www.npmjs.com/package/pkg) because its development has stopped, but we can use from [yao-pkg](https://www.npmjs.com/package/@yao-pkg/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
```