Search code examples
javascriptangulartypescriptrequire

How to use a node module in Angular which is not created for Angular?


Example module: https://www.npmjs.com/package/qrcode

It's easy for me to use it in pure javascript:

var QRCode = require('qrcode')

QRCode.toDataURL('I am a pony!', function (err, url) {
  console.log(url)
})

But I can't use "require" in Angular, right? Anguar has this:

import { X } from Y

How can I use the qrcode reader in Angular? What would the X and the Y be in this example?


Solution

  • You can install install package, along with the types for full functionality in a typescript environment such as Angular:

    npm install --save qrcode && npm install --save-dev @types/qrcode

    Then in your component you can now import * as QRCode from 'qrcode' and use it in your typescript environment.