使用pkg将带有本机节点打印机的Node.js项目打包为可执行文件

时间:2019-09-17 15:02:22

标签: node.js edgejs

我有一个节点项目,该项目将模块native-node-printer与edge-js模块结合使用(适用于Windows平台) 尝试使用pkg模块打包时,会出现此错误:

  

警告无法解决“ ../../${ process.env.NNP_PACKAGE }”     C:\ VisualSutdioCode \ ticketing-2019 \ pkg \ print-scan-server \ node_modules \ node-native-printer \ src \ windows_printer.js     动态需求可能在运行时失败,因为请求的文件     在编译时未知,不包含在可执行文件中。     使用字符串文字作为'require'的参数,或保留它     按原样,然后在“脚本”选项中指定解析的文件名。

edge-js在本机节点打印机中被引用。

有人可以帮我吗?

最好的问候

1 个答案:

答案 0 :(得分:0)

按如下所示编辑“ 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');

使用pkg打包应用程序后,将node_modules / node-native-printer / lib文件夹复制到您的应用程序根目录。您的目录结构应如下所示

  • app.exe
  • lib /

如果不更新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.

希望这会有所帮助!