How do i convert a typescript nodejs project into a single executable exe

42 views Asked by At

i am first compiling my typescript project into javascript for converting it to exe.

i tried using https://github.com/nexe/nexe https://github.com/vercel/pkg https://nwjs.io/

I am using Puppeteer which has a Puppeteer Chromium Dependency. It is causing problem

this is the log when i create the exe using pkg

PS C:\Users\mohammed mehdi\OneDrive\Desktop\myna\Automatic-Scraper\nodepuppeteer\dist> pkg .\main.js -t node18
> [email protected]
> Fetching base Node.js binaries to PKG_CACHE_PATH
  fetched-v18.5.0-win-x64             [====================] 100%

> Warning Cannot include directory %1 into executable.
  The directory must be distributed with executable as %2.
  %1: node_modules\puppeteer\.local-chromium
  %2: path-to-executable/puppeteer


PS C:\Users\mohammed mehdi\OneDrive\Desktop\myna\Automatic-Scraper\nodepuppeteer\dist> .\main-win.exe
pkg/prelude/bootstrap.js:1872
      throw error;
      ^

Error: Cannot find module 'C:\snapshot\dist\node_modules\openai\_shims\auto\runtime-node.js'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:960:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:953:15)
    at resolveExports (node:internal/modules/cjs/loader:482:14)
    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
    at Function._resolveFilename (pkg/prelude/bootstrap.js:1951:46)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at Module.require (pkg/prelude/bootstrap.js:1851:31)
    at require (node:internal/modules/cjs/helpers:102:18) {
  code: 'MODULE_NOT_FOUND',
  path: 'C:\\snapshot\\dist\\node_modules\\openai\\package.json',
  pkg: true
}

this is for nexe

nexe main.js --build
i nexe 4.0.0-rc.4
√ Node source extracted to: C:\Users\mohammed mehdi\.nexe\16.15.0
√ Compiling Node with arguments: nosign,release,x64
√ Finished in 78.68s

Error: vcbuild.bat nosign release x64 exited with code: 1

See nexe -h for usage..
1

There are 1 answers

0
Mohammed Mehdi On

I figured it out with some trial and error.

what i was doing was

  1. compiling typescript to javascript
  2. converting it to exe using pkg or nexe.

what i was missing was bundling it into a single javascript file

so now my steps are

  1. bundle the whole typescript project into a single javascript bundle.js. ( i dont need to compile typescript to javascript seperately. webpack does it for me. )
  2. convert the bundle.js into an exe using pkg ( this is very easy. )

by default pkg uses node16. you can set target node by doing this.

pkg .\main.js -t node18

after doing this the exe is easily created.

To do the webpack bundling you need to create a webpack.config.js in the root of your TS project.

in that file i have written.

// webpack.config.js
const path = require('path');

module.exports = {
  mode: 'production',
  entry: './src/main.ts', // Entry point of your application
  target: 'node',
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  }
};