node.jsangulartypescriptmercadopagomercadopagosdk

error TS2304: Cannot find name 'require'. MercadoPago


Good day everyone, I have the following problem, when compiling the project I get an error saying: ERROR in src / app / cart / cart.component.ts (63.25): error TS2304: Cannot find name ' require '. I have tried implementing npm install --save @ types / node Also checking the tsconfig.app.json configuration but still the error persists. How can I solve that? I'm developing everything with Angular. I appreciate any help

 const mercadopago = require('mercadopago');
    mercadopago.configure({
      access_token: 'Your key'
    });
    let preference = {
      items: [
        {
          title: 'Mi producto',
          unit_price: 100,
          quantity: 1,
        }
      ]
    };
    mercadopago.preferences.create(preference)
      .then(function (response) {
        this.global.init_point = response.init_point;
      }).catch(function (error) {
        console.log(error);
      });
  }

Solution

  • I think it is require. The use of require is not supported since Angular 8. You have to use es imports.

    import * as mercadopago from 'mercadopago'

    That's on one side.

    It seems to me that mercadopago is a server-side library. It might not work if it depends on nodeJS internally. Also, the fact that you are bundling your access_token is another indicator that something is wrong.