0

I am trying to make use of a newly create Node.JS module but I am getting an error saying: "Is not a function".

lib/weather.mjs

My module's code is below:
// Gets the weather of a given location
import fetch from 'node-fetch';

var latLongAirports = [{
        "name": "Madrid",
        "iata": "MAD",
        "lat": 40.49565434242003,
        "long": -3.574541319609411,
    },{
        "name": "Los Angeles",
        "iata": "LAX",
        "lat": 33.93771087455066,
        "long": -118.4007447751959,
    },{
        "name": "Mexico City",
        "iata": "MEX",
        "lat": 19.437281814699613,
        "long": -99.06588831304731,}]

export default function getTemperature (iata){
    async (dispatch) =>{
        let data = latLongAirports.find(el => el.iata === iata);
        var url = "http://www.7timer.info/bin/api.pl?lon=" + data.long + "&lat=" + data.lat + "&product=astro&output=json"
        const response = await fetch(url);
        const myJson = await response.json();
        return myJson.dataseries[0].temp2m
    }
}

And this is how I am trying to use it from my main.js

import weather from './lib/weather.mjs'
var temperature = weather.getTemperature("MEX")

What am I doing wrong here? How should I declare and use self written modules like those?

Thank you!

2
  • What is the value of type in your package.json file? Commented Oct 27, 2021 at 18:17
  • "type": "module", Commented Oct 27, 2021 at 19:09

1 Answer 1

1

weather.mjs is exporting the function. So what you get in main.js is the actual function.

In your main.js you should call the exported method like

import getTemperature from './lib/weather.mjs'
var temperature = getTemperature("MEX")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.