1.安裝原生node模塊
#全局安裝nw-gyp npm install -g nw-gyp #設置目標NW.js版本 set npm_config_target=0.31.4 #設置構建架構,ia32或x64,win10, osx爲x64操做系統 set npm_config_arch=x64 set npm_config_runtime=node-webkit set npm_config_build_from_source=true set npm_config_node_gyp=C:\Users\NALA\AppData\Roaming\npm\node_modules\nw-gyp\bin\nw-gyp.js #win10下還要設置python路徑: set PYTHON=C:\Users\NALA\.windows-build-tools\python27\python.exe 最後 npm install —msvs_version=2015
安裝編譯不成功,就多試幾回,刪除package-lock.json
2.查看python目錄:
打開咱們的cmd命令
輸入 Python
輸入 import sys
輸入 print(sys.path)javascript
3.下載Zadig:http://zadig.akeo.ie/ java
爲打印機安裝WinUSB驅動,不然報錯:LIBUSB_ERROR_NOT_SUPPORTEDnode
4.NW app項目的index.js 裏 引入下面代碼:python
const escpos = nw.require('escpos'); const device = new escpos.USB(); const options = { encoding: 'GB18030'}; let printerList = escpos.USB.findPrinter(); const printer = new escpos.Printer(device, options); console.log(printerList); device.open(function(){ printer .font('a') .align('ct') .style('bu') .size(1, 1) .text('The quick brown fox jumps over the lazy dog') .text('敏捷的棕色狐狸跳過懶狗') .barcode('1234567', 'EAN8') .qrimage('https://github.com/song940/node-escpos', function(err){ this.cut(); this.close(); }); });
5. CMD終端cd到nw sdk目錄,輸入 nw myapp 啓動客戶端git
OK,打印成功。github