paul-shuvo / nodejs-currency-converter

:currency_exchange: A NodeJS currency converter library that doesn't require subscribing to any API calls.
MIT License
33 stars 27 forks source link

SyntaxError unexpected token = #6

Closed olarotseyi closed 3 years ago

olarotseyi commented 3 years ago

When I run my backend application on localhost, it runs fine but when I deploy it to my remote server, I get an error from this package:

/node_modules/currency-converter-lt/index.js:7 currencies = { SyntaxError: Unexpected token =

I was not getting this error initially but I added the package to other files then I started getting the error which is preventing my server from working.

paul-shuvo commented 3 years ago

Could you share the node version and any other information about the error? Also, check if replacing the index.js files contents with the following code helps.

"use strict";

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const cheerio = require("cheerio");

const got = require("got"); 

class CurrencyConverter {
  constructor(params) {
    _defineProperty(this, "currencies", {
      "AFN": "Afghan Afghani",
      "ALL": "Albanian Lek",
      "DZD": "Algerian Dinar",
      "AOA": "Angolan Kwanza",
      "ARS": "Argentine Peso",
      "AMD": "Armenian Dram",
      "AWG": "Aruban Florin",
      "AUD": "Australian Dollar",
      "AZN": "Azerbaijani M anat",
      "BSD": "Bahamian Dollar",
      "BHD": "Bahraini Dinar",
      "BBD": "Bajan Dollar",
      "BDT": "Bangladeshi Taka",
      "BYR": "Belarusian Ruble",
      "BZD": "Belize Dollar",
      "BMD": "Bermudan Dollar",
      "BTN": "Bhutan currency",
      "BOB": "Bolivian Boliviano",
      "BAM": "Bosnia-Herzegovina Convertible Mark",
      "BWP": "Botswanan Pula",
      "BRL": "Brazilian Real",
      "BND": "Brunei Dollar",
      "BGN": "Bulgarian Lev",
      "BIF": "Burundian Fra nc",
      "XPF": "CFP Franc",
      "KHR": "Cambodian riel",
      "CAD": "Canadian Dollar",
      "CVE": "Cape Verdean Escudo",
      "KYD": "Cayman Islands Dollar",
      "FCFA": "Central African CFA Franc",
      "CLP": "Chilean Peso",
      "CLF": "Chilean Unit of Account (UF)",
      "CNY": "Chinese Yuan",
      "CNY": "Chinese Yuan (offshore)",
      "COP": "Colombian Peso",
      "CF": "Comorian Franc",
      "CDF": "Congolese Franc",
      "CRC": "Costa Rican Colón",
      "HRK": "Croatian Kuna",
      "CUC": "Cuban Peso",
      "CZK": "Czech Koruna",
      "DKK": "Danish Krone",
      "DJF": "Djiboutian Franc",
      "DOP": "Dominican Pe so",
      "XCD": "East Caribbean Dollar",
      "EGP": "Egyptian Pound",
      "ETB": "Ethiopian Birr",
      "FJD": "Fijian Dollar",
      "GMD": "Gambian dalasi",
      "GEL": "Georgian Lari",
      "GHS": "Ghanaian Cedi",
      "GTQ": "Guatemalan Quetzal",
      "GNF": "Guinean Franc",
      "GYD": "Guyanaese Dollar",
      "HTG": "Haitian Gourde",
      "HNL": "Honduran Lempira",
      "HKD": "Hong Kong Dollar",
      "HUF": "Hungarian Forint",
      "ISK": "Icelandic Króna",
      "INR": "Indian Rupee",
      "IDR": "Indonesian Rupiah",
      "IRR": "Iranian Rial",
      "IQD": "Iraqi Dinar",
      "ILS": "Israeli New Shekel",
      "JMD": "Jamaican Dollar",
      "JPY": "Japanese Yen",
      "JOD": "Jordanian Dinar",
      "KZT": "Kazakhstani Tenge",
      "KES": "Kenyan Shilling",
      "KWD": "Kuwaiti Dinar",
      "KGS": "Kyrgystani Som",
      "LAK": "Laotian Kip",
      "LBP": "Lebanese pound",
      "LSL": "Lesotho Loti",
      "LRD": "Liberian Dollar",
      "LYD": "Libyan Dinar",
      "MOP": "Macanese Pataca",
      "MKD": "Macedonian Denar",
      "MGA": "Malagasy Ariary",
      "MWK": "Malawian Kwacha",
      "MYR": "Malaysian Ringgit",
      "MVR": "Maldivian Rufiyaa",
      "MRO": "Mauritanian Ouguiya",
      "MUR": "Mauritian Rupee",
      "MXN": "Mexican Peso",
      "MDL": "Moldovan Leu",
      "MAD": "Moroccan Dirham",
      "MZN": "Mozambican metical",
      "MMK": "Myanmar Kyat",
      "NAD": "Namibian dol lar",
      "NPR": "Nepalese Rupee",
      "ANG": "Netherlands Antillean Guilder",
      "NZD": "New Zealand Dollar",
      "NIO": "Nicaraguan Córdoba",
      "NGN": "Nigerian Naira",
      "NOK": "Norwegian Krone",
      "OMR": "Omani Rial",
      "PKR": "Pakistani Rupee",
      "PAB": "Panamanian Balboa",
      "PGK": "Papua New Guinean Kina",
      "PYG ": "Paraguayan Guarani",
      "PHP": "Philippine peso",
      "PLN": "Poland Złoty",
      "GBP": "Pound sterling",
      "QAR": "Qatari Rial",
      "RON": "Romania n Leu",
      "RUB": "Russian Ruble",
      "RWF": "Rwandan franc",
      "SVC": "Salvadoran Colón",
      "SAR": "Saudi Riyal",
      "RSD": "Serbian Dinar",
      "SCR": "Seychellois Rupee",
      "SLL": "Sierra Leonean Leone",
      "SGD": "Singapore Dollar",
      "SBD": "Solomon Islands Dollar",
      "SOS": "Somali Shilling",
      "ZAR": "South African Rand",
      "KRW": "South Korean won",
      "VES": "Sovereign Bolivar",
      "LKR": "Sri Lankan Rupee",
      "SDG": "Sudanese pound",
      "SRD": "Surinamese Dollar",
      "SZL": "Swazi Lilangeni",
      "SEK": "Swedish Krona",
      "CHF": "Swiss Franc",
      "TJS": "Tajikistani Somoni",
      "TZS": "Tanzanian Shilling",
      "THB": "Thai Baht",
      "TOP": "Tongan Pa\"anga",
      "TTD": "Trinidad and Tobago Dollar",
      "TND": "Tunisian Dinar",
      "TRY": "Turkish lira",
      "TMT": "Turkmenistan manat",
      "UGX": "Ugandan Shilling",
      "UAH": "Ukrainian hryvnia",
      "AED": "United Arab Emirates Dirham",
      "USD": "United States Dollar",
      "UYU": "Uruguayan Peso",
      "UZS": "Uzbekistani Som",
      "VND": "Vietnamese dong",
      "XOF": "West African CFA franc",
      "YER": "Yemeni Rial",
      "ZMW": "Zambian Kwacha",
      "XBT": "Bitcoin",
      "ETH": "Ether",
      "EUR": "Euro",
      "LTC": "Litecoin",
      "TWD": "NT$",
      "PEN": "Peruvian Sol"
    });

    _defineProperty(this, "currencyCode", ["AFN", "ALL", "DZD", "AOA", "ARS", "AMD", "AWG", "AUD", "AZN", "BSD", "BHD", "BBD", "BDT", "BYR", "BZD", "BMD", "BTN", "XBT", "BOB", "BAM", "BWP", "BRL", "BND", "BGN", "BIF", "XPF", "KHR ", "CAD", "CVE", "KYD", "FCFA", "CLP", "CLF", "CNY", "CNY", "COP", "CF", "CDF", "CRC", "HRK", "CUC", "CZK", "DKK", "DJF", "DOP", "XCD", "EGP", "ETB", "FJD", "GMD", "GBP", "GEL", "GHS", "GTQ", " GNF", "GYD", "HTG", "HNL", "HKD", "HUF", "ISK", "INR", "IDR", "IRR", "IQD", "ILS", "JMD", "JPY", "JOD", "KZT", "KES", "KWD", "KGS", "LAK", "LBP", "LSL", "LRD", "LYD", "MOP", "MKD", "MGA", "MWK", "MYR", "MVR", "MRO", "MUR", "MXN", "MDL", "MAD", "MZN", "MMK", "NAD", "NPR", "ANG", "NZD", "NIO", "NGN", "NOK", "OMR", "PKR", "PAB", "PGK", "PYG ", "PHP", "PLN", "QAR", " RON", "RUB", "RWF", "SVC", "SAR", "RSD", "SCR", "SLL", "SGD", "SBD", "SOS", "ZAR", "KRW", "VES", "LKR", "SDG", "SRD", "SZL", "SEK", "CHF", "TJS", "TZS", "THB", "TOP", "TTD", "TND", "TRY", "TMT", "UGX", "UAH", "AED", "USD", "UYU", "UZS", "VND", "XOF", "YER", "ZMW", "ETH", "EUR", "LTC", "TWD", "PEN"]);

    this.currencyFrom = "";
    this.currencyTo = "";
    this.currencyAmount = 1;
    this.convertedValue = 0;

    if (params != undefined) {
      if (params["from"] !== undefined) this.from(params["from"]);
      if (params["to"] !== undefined) this.to(params["to"]);
      if (params["amount"] !== undefined) this.amount(params["amount"]);
    }
  }

  from(currencyFrom) {
    if (typeof currencyFrom !== "string") throw new TypeError("currency code should be a string");
    if (!this.currencyCode.includes(currencyFrom.toUpperCase())) throw new Error(`${currencyFrom} is not a valid currency code`);
    this.currencyFrom = currencyFrom.toUpperCase();
    return this;
  }

  to(currencyTo) {
    if (typeof currencyTo !== "string") throw new TypeError("currency code should be a string");
    if (!this.currencyCode.includes(currencyTo.toUpperCase())) throw new Error(`${currencyTo} is not a valid currency code`);
    this.currencyTo = currencyTo;
    return this;
  }

  amount(currencyAmount) {
    if (typeof currencyAmount !== "number") throw new TypeError("amount should be a number");
    if (currencyAmount <= 0) throw new Error("amount should be a positive number");
    this.currencyAmount = currencyAmount;
    return this;
  }

  rates() {
    if (this.currencyFrom === this.currencyTo) return new Promise((resolve, _) => {
      resolve(1);
    });else return got(`https://www.google.com/search?q=${this.currencyFrom}+to+${this.currencyTo}`).then(html => {
      return cheerio.load(html.body);
    }).then($ => {
      return $(".iBp4i").text().split(" ")[0];
    }).then(rates => {
      if (rates.includes(",")) rates = rates.replace(",", ".");
      return parseFloat(rates);
    });
  }

  convert(currencyAmount) {
    if (currencyAmount !== undefined) {
      this.amount(currencyAmount);
    }

    if (this.currencyFrom == "") throw new Error("currency code cannot be an empty string");
    if (this.currencyTo == "") throw new Error("currency code cannot be an empty string");
    if (this.currencyAmount == 0) throw new Error("currency amount should be a positive value");
    return this.rates().then(rates => {
      this.convertedValue = rates * this.currencyAmount;
      return this.convertedValue;
    });
  }

  currencyName(currencyCode_) {
    if (typeof currencyCode_ != "string") throw new TypeError("currency code should be a string");
    if (!this.currencyCode.includes(currencyCode_.toUpperCase())) throw new Error(`${currencyCode_} is not a valid currency code`);
    return this.currencies[currencyCode_];
  }

}

module.exports = CurrencyConverter;
olarotseyi commented 3 years ago

Node version is 12.19.0 NPM version is 6.14.8

Replacing the code in index.js with your code above might be helpful but I cannot make this replacement for my server. I think the server loads the package from your repo

paul-shuvo commented 3 years ago

I tried with the aforementioned Node and NPM version, but couldn't recreate the error.

olarotseyi commented 3 years ago

Yh.. the error only happens on my remote server. I don't have the error on my local host. I just noticed that my remote server instance with the error has a different node version, v14.15.1. Can you please check with v14.15.1 instead? Sorry for the inconvenience.

olarotseyi commented 3 years ago

My remote server is now working with the package after I updated the node version from Node LTS to Node 14 LTS (v14.15.1). I will go ahead and close the issue. Thanks for your help and for your contributions to the Nodejs community!