thiagoelg / node-printer

Native node.js printer
125 stars 76 forks source link

Cannot find module '../lib' #24

Closed josegoyo closed 3 years ago

josegoyo commented 3 years ago

Hi,

I try to use the Zebra print example but I get this error when I run npm run dev - Error: Cannot find module '../lib'.

This is my code:


var printer = require("../lib")
    , template = "N\nS4\nD15\nq400\nR\nB20,10,0,1,2,30,173,B,\"barcode\"\nP0\n";

exports.printData = async (req, res) => {

      const { code } = req.body;

      try {

          printZebra(code, "ZEBRA");

      } catch (error) {
          console.log('Error', error)
      }

      res.status(200).jsonp({ code });

}

function printZebra(barcode_text, printer_name){

    printer.printDirect({data:template.replace(/barcode/, barcode_text)
        , printer:printer_name
        , type: "RAW"
        , success:function(){
            console.log("printed: "+barcode_text);
        }
        , error:function(err){console.log(err);}
    });

}
thiagoelg commented 3 years ago

Did you clone this repository and attempted to run this code from it, or did you just copy and paste the example to somewhere else?

josegoyo commented 3 years ago

Only use npm install @thiagoelg/node-printer to install library in my node project.

these are my dependencies in package.json:

"dependencies": {
    "@thiagoelg/node-printer": "^0.5.5",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dayjs": "^1.8.28",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "jszpl": "^1.1.0",
    "moment": "^2.27.0",
    "moment-timezone": "^0.5.31",
    "morgan": "^1.10.0"
  }
thiagoelg commented 3 years ago

You should fix the way you require the library then.

require("../lib") will attempt to load the library from the relative path ../lib, which would work if running this example from this repository, but not on another project.

In your case you should change:

var printer = require("../lib")
    , template = "N\nS4\nD15\nq400\nR\nB20,10,0,1,2,30,173,B,\"barcode\"\nP0\n";

to

var printer = require("@thiagoelg/node-printer")
    , template = "N\nS4\nD15\nq400\nR\nB20,10,0,1,2,30,173,B,\"barcode\"\nP0\n";