3

I built simple command line application using commander.js for Node.js platform. Now I want to compile it to simple exe file, Which I can execute directly.

Means I want single executable file for complete application

This is my application structure

APP_ROOT
  | - package.json
  | - node_modules
  | - node_modules/.bin/myapp.bat
  | - node_modules/myapp/bin/myapp
  | - node_modules/myapp/bin/myapp-action1
  | - node_modules/myapp/bin/myapp-action2

Thanks

Farhan Yaseen
  • 2,507
  • 2
  • 22
  • 37
ssp singh
  • 152
  • 1
  • 14

2 Answers2

5
  • This is, How i packages my node.js command line app to single executable
  • Install pkg module using npm i -g pkg
  • This is my package.json File

json

{
  "name": "my-app-exe",
  "version": "1.0.0",
  "description": "Myapp-Cli tool as executable",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "myapp",
    "cli",
    "exe"
  ],
  "author": "Shisht",
  "license": "MIT",
  "devDependencies": {
    "myapp": "1.0.0"
  },
  "bin": "node_modules/myapp-cli/bin/cli",
  "pkg": {
    "assets": "node_modules/**/*"
  },
  "help": "pkg . --target host --output myapp-1.0.0-x64.exe --debug"
}
  • Command used to package myapp to myapp.exe pkg . --target host --output myapp-1.0.0-x64.exe --debug
ssp singh
  • 152
  • 1
  • 14
2

It's impossible to run a Node application without some kind of Node runtime to run it on - therefore, if you wish to distribute your program as a standalone .exe, you will have to bundle Node itself into said executable as well as your code. There are various tools that will do this for you, such as EncloseJS.

Joe Clay
  • 33,401
  • 4
  • 85
  • 85
  • Size don't matter, Yes I have to embed `node.exe` into packaged executable, But how can i? Do you know about `babel` command line tool? Can you please post working example to package it as single executable? – ssp singh Oct 20 '17 at 11:26
  • `EncloseJS` failed to install on `Windows` because failed to download resource from `https://enclosejs.s3.amazonaws.com/enclose-v0.12.15-win32-5c54091.exe.sha256` – ssp singh Oct 20 '17 at 11:28
  • @sspsingh: Babel is irrelevant here - it'll compile your JavaScript to work with older versions of the browser/Node, but it has no impact on how you'd package it. That error looks like an issue with your network. – Joe Clay Oct 20 '17 at 12:11
  • My node.js shell app is just like `babel` or any other package, which can be used on shell. So i you can try with babel, I will fix it to work with my app, that's why i asked? – ssp singh Oct 20 '17 at 16:01
  • @sspsingh: Oh, I see what you mean! There's [examples of how to package various existing command line tools in the EncloseJS GitHub](https://github.com/igorklopov/enclose/tree/master/examples) - i'd recommend using those as a starting point. – Joe Clay Oct 21 '17 at 16:48