OpenWeather API Node.js Client
openweather-api-node is a lightweight JavaScript/TypeScript wrapper that simplifies interaction with various OpenWeather API endpoints, including Current Weather, Forecast, OneCall (version 3.0), Geocoding, Historical data, and Air Pollution data. Currently at version 3.1.5, it provides a stable and actively maintained interface for integrating weather data into Node.js applications. The package has undergone significant refactoring, including a full TypeScript rewrite in v2.0.0 and the removal of Axios in v3.1.0, making it more self-contained. While there isn't a strict release schedule, the library sees regular updates for new API features and improvements. Its primary differentiator is its comprehensive support for multiple OpenWeather APIs and full TypeScript typings out-of-the-box.
Common errors
-
TypeError: OpenWeatherAPI is not a constructor
cause Attempting to instantiate the OpenWeatherAPI class incorrectly, often due to a mismatch between module import syntax (ESM vs. CommonJS) and the actual export.fixFor ESM/TypeScript, use `import OpenWeatherAPI from 'openweather-api-node'; new OpenWeatherAPI(...)`. For CommonJS, use `const { OpenWeatherAPI } = require('openweather-api-node'); new OpenWeatherAPI(...)`. -
Error: No API key provided.
cause The OpenWeatherAPI instance was initialized without a valid API key.fixEnsure the `key` property is set in the constructor options, e.g., `new OpenWeatherAPI({ key: 'YOUR_API_KEY', ... })`, or by using `weather.setKey('YOUR_API_KEY')`. -
TS2305: Module '"openweather-api-node"' has no default export.
cause A TypeScript project (likely configured for CommonJS module resolution or using an older compiler target) is attempting a default import where a named import is expected for CJS, or vice-versa with module interop settings.fixIf targeting ESM, ensure `tsconfig.json` has `"module": "ESNext"` or similar. If targeting CommonJS, use `import * as OpenWeatherAPI from 'openweather-api-node'` and then `new OpenWeatherAPI.OpenWeatherAPI(...)` or adjust `allowSyntheticDefaultImports` in `tsconfig.json`. -
Property 'weather' does not exist on type 'WeatherData' | 'OneCallData'.
cause This error can occur if the API response structure has changed, particularly after updating to v2.0.0 (camelCase changes) or v3.0.0 (getHistory return type), or if you are incorrectly accessing nested properties.fixConsult the current documentation for the exact response structure of the method you are calling. Ensure property names are `camelCase` and adjust nested accessors if the object shape has changed.
Warnings
- breaking Starting with v3.1.0, the `axios` dependency was removed. If your application relied on `axios` being a transitive dependency or expected `axios`-specific error structures, you might need to adjust your error handling or explicitly install `axios` if still needed elsewhere.
- breaking Version 3.0.0 introduced support for the new OneCall API 3.0, and the return type of the `getHistory` method was changed. If you were using the older OneCall API, you might encounter breaking changes.
- breaking Version 2.0.0 was a complete rewrite in TypeScript. Variable styles in Weather Objects changed to `camelCase`, and the `mergeWeathers` method was removed. Existing code relying on older snake_case properties or the removed method will break.
- gotcha The import style for the `OpenWeatherAPI` class differs between ESM (TypeScript/modern Node.js) and CommonJS environments. Incorrectly importing can lead to `TypeError: OpenWeatherAPI is not a constructor` or `TS2305: Module ... has no default export`.
- gotcha An API key from OpenWeatherMap is absolutely required for nearly all operations. Without a valid key, API requests will fail with authorization errors.
Install
-
npm install openweather-api-node -
yarn add openweather-api-node -
pnpm add openweather-api-node
Imports
- OpenWeatherAPI
import { OpenWeatherAPI } from 'openweather-api-node'import OpenWeatherAPI from 'openweather-api-node'
- OpenWeatherAPI
const OpenWeatherAPI = require('openweather-api-node')const { OpenWeatherAPI } = require('openweather-api-node') - OpenWeatherAPI_Options
import { OpenWeatherAPI_Options } from 'openweather-api-node'import type { OpenWeatherAPI_Options } from 'openweather-api-node'
Quickstart
import OpenWeatherAPI from "openweather-api-node";
const weather = new OpenWeatherAPI({
key: process.env.OPENWEATHER_API_KEY ?? '', // Ensure your API key is set as an environment variable
locationName: "New York",
units: "imperial"
});
weather.getCurrent().then(data => {
console.log(`Current temperature in New York is: ${data.weather.temp.cur}\u00B0F`);
}).catch(error => {
console.error("Failed to fetch current weather:", error.message);
});
weather.getForecast().then(data => {
console.log(`Forecast for New York (first entry): ${data.weather[0].temp.day}\u00B0F`);
}).catch(error => {
console.error("Failed to fetch forecast:", error.message);
});