Unhandled Rejection (TypeError): net.isIPv4 is not a function ReactJS

268 Views Asked by At

I am building an app using node-geocoder.

The file works as intended when I have it in its own file, using node.js.

But i would like to not have to use a server.

Here is my code for the node.js file where it works.

const NodeGeocoder = require("node-geocoder");
const axios = require("axios").default;
require("dotenv").config();

const options = {
  provider: "google",
  apiKey: process.env.GOOGLE_API_KEY,
};
const geocoder = NodeGeocoder(options);

const res = async function test() {
  const api_key = process.env.WEATHER_API_KEY;
  const result = await geocoder.geocode("Los Angeles, CA");
  const lat = result[0].latitude;
  const long = result[0].longitude;
  console.log(lat, long);

  const weather = axios
    .get(
      `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${long}&units=imperial&exclude=hourly,daily,minutely{part}&appid=${api_key}`
    )
    .then((response) => {
      console.log("test");
      const currentWeather = response.data.current.weather[0].main;
      const currentTemp = response.data.current.temp;
      console.log(currentWeather, currentTemp);
    });
};

res();

and here is the exact same code copied into my react app.

const NodeGeocoder = require("node-geocoder");
const axios = require("axios").default;
require("dotenv").config();

const options = {
    provider: "google",
    apiKey: process.env.GOOGLE_API_KEY,
  };
  const geocoder = NodeGeocoder(options);
  const res = async function test() {
    const api_key = process.env.WEATHER_API_KEY;
    const result = await geocoder.geocode("Los Angeles, CA");
    const lat = result[0].latitude;
    const long = result[0].longitude;
    console.log(lat, long);
  };
  res(); 

when i run the app in my browser i get a "Unhandled Rejection (TypeError): net.isIPv4 is not a function" and the error points to this code as the issue.

const result = await geocoder.geocode("Los Angeles, CA"); 

I have read online that Browserify is my answer but I am lost on implementation and if it will really even fix my issue.

0

There are 0 best solutions below