topojson Uncaught TypeError: Cannot read property 'feature' of undefined

2.2k views Asked by At

Background

  • JavaScript is failing on a call to topojson.feature(topology, object). That's the documentation for topojson-client which looks to have been added when topojson went to 2.x, but it's the same idea in 1.6.26 which is the version I'm on.
  • Using webpack to build JS for production and webpack-dev-server for local development.
  • It was working fine until fairly recently.

Problem

relevant_page.js:705 Uncaught TypeError: Cannot read property 'feature' of undefined

Code

package.json:

...
"dependencies": {
   ...
   "topojson": "^1.6.26",

relevant_page.js:

import topojson from topojson;
...
function ready(data) {
  var topojsonStates = topojson.feature(us, us.objects.states).features;

Troubleshooting

I set a breakpoint in the browser at the line that throws the error:

var topojsonStates = _topojson2.default.feature(us, us.objects.states).features;

and inspected these to see what was going on:

us: Object
  ...
  type: "Topology"

us.objects.states: Object
  ...
  type: "Topology"

_topojson2.default: undefined

_topojson2.feature(us, us.objects.states).features: Array[53]

The fact that _topojson2.default is undefined (and that _topojson2.feature works) gives me pause, but I'm not sure what's going on there. Any ideas?

Notes / update

This is distinct from how to consume an npm package with ES6 module via webpack, because there's no way to get to that question and answer if the main thing you have to go on is the Uncaught TypeError. Let me know if there's anything I should add to the question or answer to make it more useful and searchable.

1

There are 1 answers

0
Kenny Linsky On BEST ANSWER

Came across a GitHub issue where someone reported the same thing, which was fixed in a pull request with the answer, which is to import topojson as a namespace:

import * as topojson from 'topojson';

which is how the topojson-client documentation instructs you to import it. I didn't realize it would be the same for 1.6.x. Sounds like this must have broken after switching to ES6.