openexchangerates / money.js

money.js is a tiny (1kb) javascript currency conversion library, for web & nodeJS
http://openexchangerates.github.io/money.js
MIT License
1.46k stars 127 forks source link

Cannot get it to work on Node.js #31

Open bennycode opened 7 years ago

bennycode commented 7 years ago

Today I ran npm i --save money and tried to get it to work in my Node.js application:

"use strict";

const fx = require('money');
let converted = fx.convert(1000, {from: "GBP", to: "HKD"});

Unfortunately, all I get is this:

node_modules\money\money.js:78
        if ( !rates[to] || !rates[from] ) throw "fx error";
hoducha commented 7 years ago

You need to feed it with some exchange rate data and provide a base currency. For example

fx.base = "USD";
fx.rates = {
    "EUR" : 0.745101, // eg. 1 USD === 0.745101 EUR
    "GBP" : 0.647710, // etc...
    "HKD" : 7.781919,
    "USD" : 1,        // always include the base rate (1:1)
    /* etc */
}

Open Exchange Rates has an API to get these information. (Limited to 1,000 requests/month for a free account).