简体   繁体   English

使用 pkg 将带有 native-node-printer 的 Node.js 项目打包成可执行文件

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

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 :我有一个节点项目,当尝试使用 pkg 模块打包它时,它使用带有 edge-js 模块的模块 native-node-printer(用于 Windows 平台),它给出了这个错误:

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.警告无法解析 ' ../../${ process.env.NNP_PACKAGE } ' C:\\VisualSutdioCode\\ticketing-2019\\pkg\\print-scan-server\\node_modules\\node-native-printer\\src\\windows_printer.js 动态require 可能会在运行时失败,因为请求的文件在编译时未知且未包含在可执行文件中。 Use a string literal as an argument for 'require', or leave it as is and specify the resolved file name in 'scripts' option.使用字符串文字作为 'require' 的参数,或保持原样并在 'scripts' 选项中指定解析的文件名。

the edge-js is refereneced in the native-node-printer. edge-js 在 native-node-printer 中被引用。

Anyone can help me on that ?任何人都可以帮助我吗?

best regards,此致,

edit the "node_modules\\node-native-printer\\src\\windows_printer.js" as below编辑“node_modules\\node-native-printer\\src\\windows_printer.js”如下

// 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.使用 pkg 打包应用程序后,将 node_modules/node-native-printer/lib 文件夹复制到您的应用程序根目录。 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如果你不更新 dllPath 你可能会得到一个类似于下面的错误

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! print-server@1.0.0 start: `node app.js`
npm ERR! Exit status 3221225786
npm ERR!
npm ERR! Failed at the print-server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM