Packaging Node.js project with native-node-printer into an executable using pkg

787 Views Asked by At

I have a node project that uses the module native-node-printer with edge-js module (for windows platform) when trying to package it using pkg module it gives this error :

Warning Cannot resolve '../../${ process.env.NNP_PACKAGE }' C:\VisualSutdioCode\ticketing-2019\pkg\print-scan-server\node_modules\node-native-printer\src\windows_printer.js Dynamic require may fail at run time, because the requested file is unknown at compilation time and not included into executable. Use a string literal as an argument for 'require', or leave it as is and specify the resolved file name in 'scripts' option.

the edge-js is refereneced in the native-node-printer.

Anyone can help me on that ?

best regards,

1

There are 1 best solutions below

0
mushfau On

edit the "node_modules\node-native-printer\src\windows_printer.js" as below

// const edge = require(`../../${process.env.NNP_PACKAGE}`); 
const edge = require(`edge-js`);

// const dllPath = fs.realpathSync(__dirname + '/../lib/windows/windows_printer.dll').replace('.asar', '.asar.unpacked');
const dllPath = fs.realpathSync(process.cwd() + '/lib/windows/windows_printer.dll').replace('.asar', '.asar.unpacked');

after the app is packaged using pkg, copy the node_modules/node-native-printer/lib folder to your app root. your directory structure should be like as below

  • app.exe
  • lib/

if you don't updated the dllPath you probably will get an error similar to the one below

Error: Could not load file or assembly 'file:///E:\snapshot\print-server\node_modules\node-native-printer\lib\windows\windows_printer.dll' or one of its dependencies. The system cannot find the file specified.
    at Object.func (E:\snapshot\print-server\node_modules\edge-js\lib\edge.js:182:17)
    at WinPrinter.listPrinters (E:\snapshot\print-server\node_modules\node-native-printer\src\windows_printer.js:24:19)
    at E:\snapshot\print-server\app.js:9:13
    at Layer.handle [as handle_request] (E:\snapshot\print-server\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\snapshot\print-server\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (E:\snapshot\print-server\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (E:\snapshot\print-server\node_modules\express\lib\router\layer.js:95:5)
    at E:\snapshot\print-server\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (E:\snapshot\print-server\node_modules\express\lib\router\index.js:335:12)
    at next (E:\snapshot\print-server\node_modules\express\lib\router\index.js:275:10)
^Cnpm ERR! code ELIFECYCLE
npm ERR! errno 3221225786
npm ERR! [email protected] start: `node app.js`
npm ERR! Exit status 3221225786
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Hope this helps!