Currently experiencing something that is absolutely baffling. I am requiring the 'qrcode' module which I installed via npm (npm --save install qrcode
).
const {
QRCode
} = require('qrcode');
The module is listed in my package.json and does exist in node_modules. However, this is absolutely always 'undefined'. When hovering over the string in the require statement I see that it is some how referencing a node_modules directory in my local/AppData directory which I'm assuming is the global directory for modules. Using this path always results in 'undefined'. I then tried switching the path to reference the local node_modules qrcode and still 'undefined'. I've read everything here https://www.npmjs.com/package/qrcode but doesn't help with the issue I'm seeing and there appears to be not a single conversation on google about this.
TypeError: Cannot read property 'toDataURL' of undefined
When using the 'qrcode' string it is using something in the @types folder which I thought was for ES6 which I am not using. No idea how to proceed nothing adds up here, any help greatly appreciated!
Node.js example from npm package page(if only it were this easy)
var QRCode = require('qrcode')
QRCode.toDataURL('I am a pony!', function (err, url) {
console.log(url)
})
When you do this:
const {
QRCode
} = require('qrcode');
You are expecting a QRCode
property on the object that the module exports. This is called object destructing assignment in ES6. It is shorthand for and equivalent to this:
const QRCode = require('qrcode').QRCode;
And, since the object the module exports does NOT have a .QRCode
property, you get nothing but undefined
.
Instead, the top level object it exports is the QRCode
object so you need to do this:
const QRCode = require('qrcode');
If you wanted a specific property that it was exporting, you could then do something like this:
const { toDataURL } = require('qrcode');