IGC Flight Log Parser
igc-parser is a JavaScript/TypeScript library designed for parsing IGC flight log files, a standard format used for recording flights in gliders, balloons, and other unpowered aircraft. The current stable version is 2.0.0. The project maintains an active development pace, with releases occurring periodically to introduce enhancements and address breaking changes, as seen with versions 0.5.0, 1.0.0, 1.1.0, and 2.0.0. Key differentiators include its robust handling of various IGC record types (such as A, B, H, L records), support for specific extensions like LAD/LOD for B records, GeoDatum handling, and parsing of TZN timezone headers. It also offers a `lenient` parsing option, allowing processing of files even with minor errors. The library ships with comprehensive TypeScript type definitions, making it well-suited for modern TypeScript projects.
Common errors
-
TypeError: parse is not a function
cause This error typically occurs in CommonJS environments when attempting to import the `parse` function without destructuring, e.g., `const parse = require('igc-parser');` instead of `const { parse } = require('igc-parser');`.fixFor CommonJS, use object destructuring to correctly import the named `parse` export: `const { parse } = require('igc-parser');`. -
TypeError: Cannot read properties of undefined (reading 'parse')
cause This error indicates that you've likely used a default import (`import IGCParser from 'igc-parser';`) in an ESM module, but `igc-parser` does not have a default export. Thus, `IGCParser` is `undefined`, and you cannot access a property `parse` on it.fixUse a named import for the `parse` function: `import { parse } from 'igc-parser';`. -
Error: Invalid IGC format (or specific parsing error related to 'A', 'B', 'H' records, etc.)
cause The input IGC file content is malformed, corrupted, or does not strictly adhere to the IGC specification, and the parser is operating in its default strict mode.fixPass the `{ lenient: true }` option to the `parse` function to allow it to continue processing even with minor formatting errors: `parse(content, { lenient: true });`. If errors persist, thoroughly review and correct the IGC file's structure.
Warnings
- breaking Version 1.0.0 of `igc-parser` officially dropped support for Node.js versions 10 and below. Users running on these older Node.js runtimes will encounter errors and must upgrade their environment.
- breaking Version 0.5.0 introduced a more robust and possibly stricter regular expression for parsing 'A' records within IGC files. This change might cause previously parsable, but malformed, IGC files to now fail parsing in strict mode.
- gotcha `igc-parser` provides its primary functionality, including the `parse` function, as named exports. Attempting to use a default import (`import IGCParser from 'igc-parser';`) will result in `undefined` for `IGCParser` and subsequent runtime errors when trying to call its methods.
Install
-
npm install igc-parser -
yarn add igc-parser -
pnpm add igc-parser
Imports
- parse
import IGCParser from 'igc-parser';
import { parse } from 'igc-parser'; - parse (CommonJS, destructuring)
const parse = require('igc-parser');const { parse } = require('igc-parser'); - parse (CommonJS, object access)
const { IGCParser } = require('igc-parser');const IGCParser = require('igc-parser'); // ... let result = IGCParser.parse(fileContent); - IGCFile (TypeScript Type)
import { IGCFile } from 'igc-parser';
Quickstart
import { parse } from 'igc-parser';
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
// Example IGC content (simplified for brevity, typically much longer)
const igcContent = `AXXXLXNAV;LX8000,FW:2.0\nHFDTE150717\nHFCIDJohn Doe\nHOTYPESHW-222\nHOREGISTRATIOND-2019\nI013638FXA\nB1018265101070N00701006E00049000-42\nB1018275101071N00701007E00050000-43\nLGC-2023-01-01T12:00:00Z\n60DC059E2D2F6CAD2E889224E355DBDDB805CAB100000639B49FC5F280A292C990F554789F12381380720000`;
// Create a temporary file to demonstrate reading from disk
const tempFilePath = join(__dirname, 'temp.igc');
writeFileSync(tempFilePath, igcContent, 'utf8');
try {
const fileContent = readFileSync(tempFilePath, 'utf8');
// Parse the IGC file content, allowing for minor errors with lenient mode
const result = parse(fileContent, { lenient: true });
console.log('Successfully parsed IGC file:');
console.log(`Date: ${result.date}`);
console.log(`Pilot: ${result.pilot}`);
console.log(`Glider Type: ${result.gliderType}`);
console.log(`Number of Fixes: ${result.fixes.length}`);
console.log(`First Fix Time: ${result.fixes[0]?.time}`);
console.log(`Last Fix Time: ${result.fixes[result.fixes.length - 1]?.time}`);
} catch (error) {
console.error('Error parsing IGC file:', error);
} finally {
// In a production application, you might remove the temporary file here.
// import { unlinkSync } from 'fs';
// unlinkSync(tempFilePath);
}