<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sudo Space</title>
    <description>The latest articles on Forem by Sudo Space (@sudospace).</description>
    <link>https://forem.com/sudospace</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1598850%2Fe768b92d-80d5-4fba-bcab-95a2a0fa24b5.jpg</url>
      <title>Forem: Sudo Space</title>
      <link>https://forem.com/sudospace</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sudospace"/>
    <language>en</language>
    <item>
      <title>Compile your NodeJS application to single file executable</title>
      <dc:creator>Sudo Space</dc:creator>
      <pubDate>Sun, 09 Jun 2024 13:59:07 +0000</pubDate>
      <link>https://forem.com/sudospace/compile-your-nodejs-application-to-single-file-executable-5aoe</link>
      <guid>https://forem.com/sudospace/compile-your-nodejs-application-to-single-file-executable-5aoe</guid>
      <description>&lt;p&gt;Hi great developers :)&lt;br&gt;
In this article, I am trying to share with you my small experience about converting nodeJS projects to single file executable.&lt;br&gt;
In short, there are methods that you can use.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;NodeJS single file executable built-in feature&lt;br&gt;
Here I link &lt;a href="https://nodejs.org/api/single-executable-applications.html"&gt;NodeJS documentation&lt;/a&gt;. 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 &lt;code&gt;Segmentation fault (core dumped)&lt;/code&gt;. I tested the same steps on Windows and there was no problem with the implementation and it worked well.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bun&lt;br&gt;
You can use &lt;a href="https://bun.sh/docs/bundler/executables"&gt;Bun&lt;/a&gt; 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 &lt;code&gt;node:fs.WriteStream&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deno&lt;br&gt;
I have not tried Deno. But you can read its &lt;a href="https://docs.deno.com/runtime/manual/tools/compiler"&gt;documentation&lt;/a&gt;. Of course, I don't think Deno is fully compatible with nodeJS packages. (as I understood from its documentation)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The method I used and it worked&lt;br&gt;
I used pkg. Of course, I don't mean &lt;a href="https://www.npmjs.com/package/pkg"&gt;vercel pkg&lt;/a&gt; because its development has stopped, but we can use from &lt;a href="https://www.npmjs.com/package/@yao-pkg/pkg"&gt;yao-pkg&lt;/a&gt;. It's an active fork of vercel pkg that also supports node20.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's implement an example together:&lt;/p&gt;

&lt;p&gt;Make a folder and create a file as &lt;code&gt;package.json&lt;/code&gt; in that :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "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"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;make a file as &lt;code&gt;tsconfig.json&lt;/code&gt; with this content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "compilerOptions": {
      "target": "es2022",
      "lib": ["es2022"],
      "module": "commonjs",
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
      "strict": true,
      "skipLibCheck": true,
      "rootDir": "./src",
      "outDir": "./dist"
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make &lt;code&gt;src&lt;/code&gt; folder and create file as &lt;code&gt;app.ts&lt;/code&gt; in that :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import express from "express";

import router from "./routes/router";

const app = express();

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

app.listen(3000, () =&amp;gt; {
  console.log("SERVER IS RUNNING...");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make a folder as &lt;code&gt;routes&lt;/code&gt; in src and create a file as &lt;code&gt;router.ts&lt;/code&gt; in that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Router } from "express";

const router = Router();

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

export default router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install npm packages :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run these commands to compile your project to single file executable :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build-ts
npm run build-linux-x64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run executable file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./app-linux-x64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
