node-geo-tz: Error while running typescript node using geo-tz in build file

I am using geo-tz in our typescript node application for getting the geolocation details. When i am running the node server locally, it’s working fine but when taken a node server build , i am getting an error like the below while calling geo-tz function like this:

import * as  geoTz from 'geo-tz';
const testGeotz= ()=>{
       try{
           const coords = [110,-20]
            let name = geoTz(coords[1], coords[0])[0];
           }catch (e) {
               console.log(e);
           }
}

image

Why is it happening like this ? My node version - 12.16.3 geo-tz version - 6.0.0

I even tried downgrading the geo-tz version and tried to upgrade to latest v6.0.1 .Still same response. Please tell me whether I have missed out any dependency . @evansiroky @greenkeeperio-bot

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (3 by maintainers)

Most upvoted comments

We are using nx workspace for running the app and building it . Building will transpile typeScript file into JavaScript (under the hood, its what tsc does)…

This is the issue. node-geo-tz lazy-loads the underlying data by default to attempt to not use so much memory. In whatever build process that is being used, all of the data files must still be able to be referenced as these include the precise data about the timezone boundaries. The reason the error occurs for only certain coordinates is that most coordinates will fall into areas that have the timezones pre-calculated and thus don’t require loading the underlying geographic data to perform an exact lookup near a boundary with the timezones.

I’m going to leave this issue open as I am not sure what the best way to provide guidance is for this use case. The README already states that this package cannot be used in the browser due to loading a lot of data, but it appears that users are trying to build/transpile/whatever on the server-side too. I am not exactly sure what build systems everybody is using and there might be many, but if there is a way to instruct the build process to include every file within the data folder of this project in a way that those files can be required as needed, that should work.

I know I am late to the party here but I may have discovered this bug by accident from a different issue I was having. We were having one issue when trying to use the find function on the frontend web app, and the above issue when attempting to use it in a Lambda. The problem arose from using a lat/long combo with differing decimal point accuracies.

lat: 34.05861 long: -118.3928

Notice 5 and 4 decimal places, respectively. We ended up rounding them to the nearest whole integer and it solved both of the issues. Something weird happens when the function receives a lat and long with differing decimal point lengths. Not sure if it helps, but just something that worked for us.

Great suggestion, but it doesn’t apply to my situation. I’m executing my code on a NextJs API hosted on Vercel

“geo-tz”: “^8.0.1”

…Failed to get timezone: Error: ENOENT: no such file or directory, open '/var/task/node_modules/geo-tz/data/timezones-1970.geojson.geo.dat'

export const getTimeZoneByGeo = (lat: number, lng: number) => {
  try {
    const timezones = find(Number(lat.toFixed(4)), Number(lng.toFixed(4)))
    return (timezones.length && timezones[0]) || 'UTC'
  } catch (error) {
    console.error(`Failed to get timezone: ${error}`)
    return 'UTC'
  }
}

Error: ENOENT: no such file or directory, open ‘/var/task/node_modules/geo-tz/data/timezones-now.geojson.geo.dat’. REQUEST LAT 31.979672370612 LNG 34.752403290084

This problem is happening here too.

When I use locally, It works. But, when it is build, that happen.

I know I am late to the party here but I may have discovered this bug by accident from a different issue I was having. We were having one issue when trying to use the find function on the frontend web app, and the above issue when attempting to use it in a Lambda. The problem arose from using a lat/long combo with differing decimal point accuracies.

lat: 34.05861 long: -118.3928

Notice 5 and 4 decimal places, respectively. We ended up rounding them to the nearest whole integer and it solved both of the issues. Something weird happens when the function receives a lat and long with differing decimal point lengths. Not sure if it helps, but just something that worked for us.

I have two thoughts on this.

  • Can someone provide a sample point that triggers this condition so we can test for it?
  • Perhaps the library could check for the file on module initialization and error or warn if it’s not there. It’s weird to encounter a missing file bug 100k records into a process.

This problem is happening here too.

When I use locally, It works. But, when it is build, that happen.

Are you using typescript node or normal nodejs ? @miguelpfitscher