A Node.js module for fetching weather data from multiple public APIs with a standardized interface.
- AccuWeather ✅
- OpenWeatherMap ✅
- WeatherBit ✅
DarkSky⚠️ (Deprecated - Apple shut down the API in 2023)Weather Underground❌ (API deprecated)
This module provides a standardized weather object regardless of which API you use. This approach offers several key benefits:
- Future-proof your app - Seamlessly switch providers when APIs get deprecated (like DarkSky and Weather Underground)
- Cost optimization - Switch providers based on pricing or feature changes
- Increased reliability - Use multiple providers simultaneously to work around rate limits
- Consistent interface - Same code works with any supported provider
npm install jw-weatherconst weather = require('jw-weather');
const boston = new weather.service({
provider: 'openweathermap',
key: 'your-api-key-here',
latitude: 42.3601,
longitude: -71.0589,
celsius: false // optional, defaults to Fahrenheit
});
// Wait for the service to be ready
boston.on('ready', async () => {
try {
await boston.update();
console.log(`Current temperature: ${boston.temp}°F`);
console.log(`Conditions: ${boston.currentCondition}`);
} catch (error) {
console.error('Failed to get weather:', error);
}
});
// Handle errors
boston.on('error', (error) => {
console.error('Weather service error:', error);
});| Option | Type | Required | Description |
|---|---|---|---|
provider |
string | Yes | Weather service: 'accuweather', 'openweathermap', 'weatherbit' |
key |
string | Yes | API key from your chosen provider |
latitude |
number | Yes | Location latitude |
longitude |
number | Yes | Location longitude |
celsius |
boolean | No | Temperature unit (default: false for Fahrenheit) |
After calling update(), the following properties are available:
temp- Current temperaturefeelsLike- "Feels like" temperaturehumidity- Humidity percentage (0-1)currentCondition- Text description of current conditionsicon- Icon identifier from the providersunrise- Sunrise time (Date object)sunset- Sunset time (Date object)
lastUpdate- When the data was last fetched locallyforecastTime- Actual forecast time from the providerraw- Complete raw response from the provider
forecast- Array of daily forecast objects
Each forecast day includes:
tempHigh/tempLow- High/low temperaturesfeelsLikeHigh/feelsLikeLow- "Feels like" temperatureshumidity- Humidity (Note: AccuWeather doesn't provide this for forecasts)condition- Weather condition descriptionicon- Weather icon identifiersunrise/sunset- Sun times
Fetches the latest weather data from your configured provider.
// Using async/await
try {
await boston.update();
console.log('Weather updated successfully');
} catch (error) {
console.error('Update failed:', error);
}
// Using Promises
boston.update()
.then(() => {
console.log('Weather updated successfully');
})
.catch((error) => {
console.error('Update failed:', error);
});
// Using callback
boston.update((error) => {
if (error) {
console.error('Update failed:', error);
} else {
console.log('Weather updated successfully');
}
});Returns a complete weather data object with all available information.
const completeWeather = boston.fullWeather();
console.log(JSON.stringify(completeWeather, null, 2));const weather = require('jw-weather');
const myLocation = new weather.service({
provider: 'openweathermap',
key: 'your-api-key-here',
latitude: 40.7128,
longitude: -74.0060,
celsius: true
});
myLocation.on('ready', async () => {
try {
await myLocation.update();
// Current conditions
console.log(`Temperature: ${myLocation.temp}°C`);
console.log(`Feels like: ${myLocation.feelsLike}°C`);
console.log(`Humidity: ${Math.round(myLocation.humidity * 100)}%`);
console.log(`Conditions: ${myLocation.currentCondition}`);
// 5-day forecast
console.log('\n5-Day Forecast:');
myLocation.forecast.forEach((day, index) => {
console.log(`Day ${index + 1}: ${day.tempHigh}°/${day.tempLow}° - ${day.condition}`);
});
} catch (error) {
console.error('Weather update failed:', error);
}
});
myLocation.on('error', (error) => {
console.error('Service error:', error);
});- OpenWeatherMap: Sign up here (free tier available)
- AccuWeather: Register here (free tier available)
- WeatherBit: Get API key here (free tier available)
- Location format: All providers use latitude/longitude coordinates for consistency, even though some support other formats natively
- Rate limits: Each provider has different rate limits - check their documentation
- Forecast accuracy: OpenWeatherMap's free tier provides 3-hour forecasts, while others provide daily forecasts
For more examples, see demo.js in this repository.