How to load a .geojson file in TypeScript with webpack?

Viewed 57

I need data from a .geojson file. I tried two things:

const geojson = require('../../assets/mygeojson.geojson');

This failed with the following error:

Module parse failed: Unexpected token (2:6) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

My second approach was to use the following:

const geojson = await (await fetch('../../assets/mygeojson.geojson')).json();

This failed with the following error:

Module parse failed: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it) File was processed with these loaders:

  • ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js
  • ./node_modules/@ngtools/webpack/src/ivy/index.js

I don't know where exactly to enable those experiments mentioned in the error message.

In any event, all I need is the GeoJSON file as a plain JSON object for further use in my code. Is there perhaps a third approach so simply load a file in TypeScript?

1 Answers

For me, the solution of this answer solved the problem:

{
    test: /\.geojson$/,
    type: 'json',
}
Related