Search code examples
node.jsyarnpkg

How to include Node.js native modules while compiling to .exe


I'm using pkg to compile my Node.js project into an executable. The project includes a native module called "node-printer".

Pkg has problems compiling this module, because when i run the executable i get this error:

pkg/prelude/bootstrap.js:1359
  throw error;
  ^
.....
(internal/modules/cjs/loader.js:1218:10),Module.load (internal/modules/cjs/loader.js:1047:32)] {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\snapshot\\ckiosk\\node_modules\\printer\\lib\\printer.js',
'C:\\snapshot\\ckiosk\\controllers\\receiptController.js',
'C:\\snapshot\\ckiosk\\api\\ajax.js',
'C:\\snapshot\\ckiosk\\webApp.js',
'C:\\snapshot\\ckiosk\\main.js'
]

This is from the pkg readme:

Native addons (.node files) use is supported. When pkg encounters a .node file in a require call, it will package this like an asset. In some cases (like with the bindings package), the module path is generated dynamicaly and pkg won't be able to detect it. In this case, you should add the .node file directly in the assets field in package.json.

Here is the pkg in my package.json file

"pkg": {
"scripts": [
  "config/*.js"
],
"assets": [
  "views/*",
  "views/**/*",
  "config/*",
  "public/assets/**/*",
  "node_modules/printer/lib/node_printer.node"

],
"targets": [
  "node14-win"
]},

As you can see, i've included the node printer .node file in the assets, but the result is still the same. What am i missing?


Solution

  • Turns out i have been using the wrong command to build. I used this

    pkg "main.js" -t node14-win -o  build/ckiosk.exe
    

    But to make use of the configuration in package.json we have to use

    pkg .