编辑“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 文件夹复制到您的应用程序根目录。您的目录结构应如下所示
如果您不更新 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.
希望这可以帮助!