Using jsbarcode in my node.js web application I am unable to get rid of error: JsBarcode is not defined
.
In my .ejs
file I have the following code:
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.8.0/dist/JsBarcode.all.min.js"></script>
</head>
<body>
<svg id="barcode"></svg>
<% JsBarcode("#barcode", obj[0].num); %>
</body>
</html>
I'm confused as to why JsBarcode
is undefined? I have tried making sure jsbarcode
is installed via npm install jsbarcode --save
and in the head of my .ejs
file I've included the cdn <script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.8.0/dist/JsBarcode.all.min.js"></script>
First, EJS is a templating engine, so packages you install via NPM would not work in this case (except you inject the content).
In your scenario you could treat it like any other HTML page. Just source the library using the script
tag and include your code afterward
// template.ejs
<svg id="barcode"></svg>
<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.8.0/dist/JsBarcode.all.min.js"></script>
<script>
JsBarcode('#barcode', <%- obj[0].num %>)
</script>
Note that there is also a newer version of JSBarcode
available, so I guess it's also worth updating.
<script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.4/dist/JsBarcode.all.min.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.