Node Geocoder
raw JSON →Node Geocoder is a flexible geocoding and reverse geocoding library for Node.js, providing a unified API to various geocoding services. It currently stands at version 4.4.1 and sees a stable release cadence. Key differentiators include its extensive support for over a dozen providers such as Google Maps, MapQuest, OpenStreetMap, HERE, and ArcGIS Online, allowing developers to switch services without major code changes. The library abstracts away provider-specific API quirks, offering consistent input and output formats. It supports both callback and Promise-based usage, making it adaptable to modern asynchronous JavaScript patterns. Developers must supply their own API keys for most commercial providers, as well as handle potential rate limiting and usage quotas.
Common errors
error Error: Missing API key for provider [providerName] ↓
apiKey property of the geocoder options object. error TypeError: geocoder.geocode is not a function ↓
const geocoder = NodeGeocoder(options);. The NodeGeocoder export itself is the factory function, not the geocoder instance. error status is REQUEST_DENIED. You must use an API key to authenticate each request to Google Maps Platform APIs. ↓
error SyntaxError: await is only valid in async functions and the top level bodies of modules ↓
async function and await the results, then call that async function, or ensure your file is configured as an ESM module if using top-level await. Warnings
gotcha Most geocoding providers, especially commercial ones like Google, HERE, and MapQuest, require an API key to function. Requests without a valid key will likely be denied. ↓
gotcha Geocoding services typically enforce rate limits or usage quotas. Exceeding these limits can lead to temporary or permanent service denials, often returning `OVER_QUERY_LIMIT` or similar errors. ↓
gotcha While `node-geocoder` requires Node.js >=18, its official usage examples in v4.4.1's README utilize CommonJS `require()`. Directly using ESM `import` statements might lead to module resolution issues depending on your project configuration and Node.js environment, as it might primarily expose a CJS module. ↓
gotcha API keys with HTTP referrer restrictions might fail for server-side usage (e.g., in Node.js applications deployed to cloud functions or servers). Many server-side calls expect IP address restrictions or no restrictions. ↓
gotcha The library's `fetch` option allows a custom HTTP client. If not provided, it relies on the global `fetch` API. In Node.js environments older than 18, `fetch` might not be globally available, leading to runtime errors if no custom `fetch` implementation is supplied. ↓
Install
npm install node-geocoder yarn add node-geocoder pnpm add node-geocoder Imports
- NodeGeocoder wrong
import NodeGeocoder from 'node-geocoder'; import { NodeGeocoder } from 'node-geocoder';correctconst NodeGeocoder = require('node-geocoder'); - NodeGeocoder (Instantiation)
const geocoder = NodeGeocoder(options); - Options wrong
const options = { provider: 'google' };correctconst options = { provider: 'google', apiKey: 'YOUR_API_KEY' // Mandatory for most providers };
Quickstart
const NodeGeocoder = require('node-geocoder');
const nodeFetch = require('node-fetch'); // Required for custom fetch example, otherwise global fetch is used in Node 18+
const API_KEY_GOOGLE = process.env.GOOGLE_GEOCODER_API_KEY ?? '';
const API_KEY_HERE = process.env.HERE_GEOCODER_API_KEY ?? '';
const optionsGoogle = {
provider: 'google',
apiKey: API_KEY_GOOGLE,
formatter: null // Optional: 'gpx', 'string', etc.
};
const optionsHere = {
provider: 'here',
apiKey: API_KEY_HERE,
language: 'en' // Optional, specify language for results
};
const geocoderGoogle = NodeGeocoder(optionsGoogle);
const geocoderHere = NodeGeocoder(optionsHere);
async function runGeocoding() {
try {
console.log('--- Google Geocoding ---');
const resGoogle = await geocoderGoogle.geocode('29 champs elysée paris');
console.log('Geocode (Google):', resGoogle[0].formattedAddress);
console.log('\n--- Here Reverse Geocoding ---');
const resHere = await geocoderHere.reverse({ lat: 45.767, lon: 4.833 });
console.log('Reverse Geocode (HERE):', resHere[0].formattedAddress);
console.log('\n--- Google Advanced Geocoding ---');
const resAdvanced = await geocoderGoogle.geocode({
address: '29 champs elysée',
country: 'France',
zipcode: '75008'
});
console.log('Advanced Geocode (Google):', resAdvanced[0].formattedAddress);
console.log('\n--- Batch Geocoding (Google) ---');
const batchResults = await geocoderGoogle.batchGeocode([
'13 rue sainte catherine, bordeaux',
'another address, london'
]);
console.log('Batch Geocode (Google) Result 1:', batchResults[0][0].formattedAddress);
console.log('Batch Geocode (Google) Result 2:', batchResults[1][0].formattedAddress);
} catch (error) {
console.error('Geocoding error:', error);
}
}
runGeocoding();