Laurence Cope
Laurence Cope

Reputation: 422

How to get this Maxmind Node.js module working in Next.js? I get error TypeError: lookup.get is not a function

I have installed node-maxmind in my React/Next.js app: https://www.npmjs.com/package/maxmind

But when adding the code it states, I get the error:

TypeError: lookup.get is not a function

I have tried various variations of the code and where I add it, but it always shows the same error. I can only imagine its in the Node module itself, but no-one else reports the problem in GitHub, so assume perhaps it's not compatible with React/Next.js.

Code is as follows and I have replaced the path to the mmdb file correct:

import maxmind, { CityResponse } from 'maxmind';

const lookup = await maxmind.open<CityResponse>('/path/to/GeoLite2-City.mmdb');
console.log(lookup.get('66.6.44.4')); // inferred type maxmind.CityResponse

console.log(lookup.getWithPrefixLength('66.6.44.4')); // tuple with inferred type [maxmind.CityResponse|null, number]

If I use the Reader code, then I get a different error. So this code:

import { Reader } from 'maxmind';
const buffer = fs.readFileSync('./db.mmdb');
const lookup = new Reader<CityResponse>(buffer);
const city = lookup.get('8.8.8.8');

const [city2, prefixLength] = lookup.getWithPrefixLength('66.6.44.4');

Results in this error:

TypeError: The "original" argument must be of type Function

Upvotes: -1

Views: 851

Answers (1)

Laurence Cope
Laurence Cope

Reputation: 422

It looks like this is a server side module not client side. I don't think it's clear on the Maxmind site (I am not familiar enough with Node/Next to just know it!).

So the following code works when run server side in getServerSideProps:

import React from "react";

import maxmind, { CountryResponse } from 'maxmind';

const Country = (props) => {

    return (
    <>
        Country: {props.country.country.names.en}
    </>
    );
};

export default Country;


export async function getServerSideProps () {
    
    const lookup = await maxmind.open('/path/to/GeoLite2-City.mmdb');
    const country = lookup.get('66.6.44.4');

    return {
        props: {
            country: country
        }
    };

}

Upvotes: 0

Related Questions