{"id":13219,"library":"geostyler-geojson-parser","title":"GeoStyler GeoJSON Parser","description":"The `geostyler-geojson-parser` package provides an implementation of the GeoStyler data parser specification specifically for GeoJSON. It enables conversion between standard GeoJSON objects and GeoStyler's internal style representation, facilitating the styling of geographic data. The current stable version is 2.0.0, released recently after a significant architectural change that embraced ECMAScript Modules (ESM). While the project's release cadence has been irregular, v2.0.0 signifies a modernization effort for its module distribution. Key differentiators include its tight integration within the broader GeoStyler ecosystem, offering a consistent API for managing styles across various data formats, and robust handling of diverse GeoJSON structures into a unified style object. It is a fundamental component for applications that need to dynamically style GeoJSON data based on GeoStyler definitions.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/geostyler/geostyler-geojson-parser","tags":["javascript","geostyler","parser","data","geojson","typescript"],"install":[{"cmd":"npm install geostyler-geojson-parser","lang":"bash","label":"npm"},{"cmd":"yarn add geostyler-geojson-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add geostyler-geojson-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since version 2.0.0, the package primarily exports ESM. CommonJS `require()` syntax for named exports can be problematic or require specific bundler configurations. Use the direct package name, as the internal `dist` path is no longer exposed for direct import.","wrong":"const GeoJsonParser = require('geostyler-geojson-parser').GeoJsonParser;","symbol":"GeoJsonParser","correct":"import { GeoJsonParser } from 'geostyler-geojson-parser';"},{"note":"While `GeoJsonParser` is primarily a named export, some tooling or configurations might interpret it as a default. However, direct imports from the 'dist' folder (e.g., `/dist/GeoJsonParser`) are deprecated and will break imports in v2.0.0+.","wrong":"import { GeoJsonParser } from 'geostyler-geojson-parser/dist/GeoJsonParser';","symbol":"GeoJsonParser (as default)","correct":"import GeoJsonParser from 'geostyler-geojson-parser';"},{"note":"This pattern allows access to all named exports, e.g., `GeoJsonParserModule.GeoJsonParser`. The CommonJS `require` call will return the module object but might not correctly expose the ESM exports or might provide an unexpected structure due to the v2.0.0 ESM transition.","wrong":"const GeoJsonParserModule = require('geostyler-geojson-parser');","symbol":"All exports as namespace","correct":"import * as GeoJsonParserModule from 'geostyler-geojson-parser';"}],"quickstart":{"code":"import { GeoJsonParser } from 'geostyler-geojson-parser';\nimport { Style } from 'geostyler-style'; // GeoStyler-style types are typically needed for parsing output\n\nconst geoJsonParser = new GeoJsonParser();\n\nconst sampleGeoJson = {\n  \"type\": \"FeatureCollection\",\n  \"features\": [\n    {\n      \"type\": \"Feature\",\n      \"properties\": {\n        \"name\": \"Example Point\",\n        \"category\": \"A\"\n      },\n      \"geometry\": {\n        \"type\": \"Point\",\n        \"coordinates\": [10.2, 53.5]\n      }\n    },\n    {\n      \"type\": \"Feature\",\n      \"properties\": {\n        \"name\": \"Example Polygon\",\n        \"area\": 123.45\n      },\n      \"geometry\": {\n        \"type\": \"Polygon\",\n        \"coordinates\": [[[-10, 0], [0, 10], [10, 0], [0, -10], [-10, 0]]]\n      }\n    }\n  ]\n};\n\nasync function parseAndConvertGeoJson() {\n  try {\n    console.log('Attempting to parse GeoJSON...');\n    // Convert GeoJSON to GeoStyler Style object\n    const geoStylerStyle: Style = await geoJsonParser.readStyle(sampleGeoJson);\n    console.log('Successfully parsed to GeoStyler Style:\\n', JSON.stringify(geoStylerStyle, null, 2));\n\n    console.log('\\nAttempting to convert GeoStyler Style back to GeoJSON...');\n    // Convert GeoStyler Style object back to GeoJSON\n    const convertedGeoJson = await geoJsonParser.writeStyle(geoStylerStyle);\n    console.log('Successfully converted back to GeoJSON:\\n', JSON.stringify(convertedGeoJson, null, 2));\n\n  } catch (error) {\n    console.error('Error during GeoJSON parsing or conversion:', error);\n  }\n}\n\nparseAndConvertGeoJson();","lang":"typescript","description":"This snippet demonstrates how to instantiate the `GeoJsonParser` and use its `readStyle` method to convert a GeoJSON object into a GeoStyler `Style` object, and `writeStyle` for the reverse conversion. This showcases typical usage for styling workflows."},"warnings":[{"fix":"Migrate your project to use ES modules (`import`/`export`) or configure your bundler (e.g., Webpack, Rollup) to correctly handle ESM modules. For Node.js, ensure your `package.json` specifies `\"type\": \"module\"` or use `.mjs` file extensions for ESM code.","message":"Version 2.0.0 introduces a significant change by moving to an ECMAScript Module (ESM) build. This means `require()` statements in CommonJS environments might no longer work directly, leading to `ERR_REQUIRE_ESM` errors.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Update your import paths to directly reference the package name, e.g., `import { GeoJsonParser } from 'geostyler-geojson-parser';`. The package exports are now configured to be resolved from the root.","message":"The internal file structure changed in v2.0.0; all exported files are now located inside the `dist` folder, but direct imports to these paths are deprecated. Direct imports pointing to specific files within `geostyler-geojson-parser/dist/...` paths will now break.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade your Node.js environment to version 20.6.0 or newer to ensure full compatibility and prevent unexpected runtime errors.","message":"Version 2.0.0 and later require Node.js version 20.6.0 or higher. Running with older Node.js versions will result in compatibility issues or failures due to engine requirements.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Switch to `import` statements and ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json` or using `.mjs` file extensions for files containing `import`/`export`).","cause":"Attempting to use `require()` to import an ESM-only module (`geostyler-geojson-parser@2.0.0+`) in a CommonJS context.","error":"ERR_REQUIRE_ESM"},{"fix":"Remove `/dist/GeoJsonParser` from your import path. The correct import should be `import { GeoJsonParser } from 'geostyler-geojson-parser';`.","cause":"Incorrect import path after v2.0.0, which deprecated direct access to files within the `dist` folder.","error":"Cannot find module 'geostyler-geojson-parser/dist/GeoJsonParser' or its corresponding type declarations."},{"fix":"For ESM, use `import { GeoJsonParser } from 'geostyler-geojson-parser';`. If you are strictly in a CommonJS environment, check if a specific CommonJS build is provided or consider a dynamic import (`import('geostyler-geojson-parser')`). Otherwise, ensure you are correctly accessing the named export, e.g., `const GeoJsonParser = require('geostyler-geojson-parser').GeoJsonParser;` (though this might still fail if the package is truly ESM-only for its primary exports).","cause":"CommonJS `require()` might return the module object directly, rather than the class constructor itself, especially when dealing with mixed ESM/CJS or incorrect default/named export handling after the v2.0.0 transition.","error":"TypeError: GeoJsonParser is not a constructor"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}