uber / nebula.gl

A suite of 3D-enabled data editing overlays, suitable for deck.gl

Home Page:https://nebula.gl/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Super expression must either be null or a function

cubocicloide opened this issue · comments

Using the latest versions of nebula.gl/deck.gl with react 18.2.0 to reproduce the official Hello World example is giving this error:

react.development.js:1407 Uncaught TypeError: Super expression must either be null or a function
    at _inherits (nebula-layer.ts:2:1)
    at nebula-layer.ts:6:22
    at node_modules/nebula.gl/dist-es6/lib/nebula-layer.js (nebula-layer.ts:33:4)
    at __require2 (chunk-S5KM4IGW.js?v=e7517ec1:18:50)
    at node_modules/nebula.gl/dist-es6/index.js (index.ts:7:1)
    at __require2 (chunk-S5KM4IGW.js?v=e7517ec1:18:50)
    at dep:nebula__gl:1:16

Here an abstract of my dependencies:

{
  "dependencies": {
    "@deck.gl/core": "^8.8.12",
    "@deck.gl/extensions": "^8.8.12",
    "@deck.gl/geo-layers": "^8.8.12",
    "@deck.gl/layers": "^8.8.12",
    "@deck.gl/mapbox": "^8.8.12",
    "@deck.gl/mesh-layers": "^8.8.12",
    "@deck.gl/react": "^8.8.12",
    "@loaders.gl/core": "^3.2.9",
    "@luma.gl/core": "^8.5.17",
    "nebula.gl": "^1.0.4",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router": "^6.4.1",
    "react-router-dom": "^6.4.1"
  }
}

I am checking and the problem is not the new version of react. I am using vite.js (not create-react-app) as a bundle manager. It is throwing that error that basically is caused by this js module:

import Event from 'events';
import { v4 } from 'uuid';

import Feature from './feature';

export default class NebulaLayer extends Event.EventEmitter {
  getData: () => Record<string, any>[];
  toNebulaFeature: (data: Record<string, any>) => Feature;
  id: string;
  helperLayers: Record<string, any>[];

  // flags
  usesMapEvents = false;
  enablePicking = false;
  enableSelection = false;
  //

  constructor({ getData, on, toNebulaFeature }: Record<string, any>) {
    super();
    this.id = v4();
    this.getData = getData;
    this.toNebulaFeature = toNebulaFeature;
    this.helperLayers = [];

    if (on) {
      // @ts-ignore
      Object.keys(on).forEach((key) => this.on(key, on[key]));
    }
  }

  render(config: Record<string, any>): unknown {
    return null;
  }
}

When this file is compiled, it goes to node_modules/nebula.gl/dist-es6/lib/nebula-layer.js, converted into this:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _events = _interopRequireDefault(require("events"));

var _uuid = require("uuid");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var NebulaLayer = /*#__PURE__*/function (_Event$EventEmitter) {
  _inherits(NebulaLayer, _Event$EventEmitter);

  var _super = _createSuper(NebulaLayer);

  // flags
  //
  function NebulaLayer(_ref) {
    var _this;

    var getData = _ref.getData,
        on = _ref.on,
        toNebulaFeature = _ref.toNebulaFeature;

    _classCallCheck(this, NebulaLayer);

    _this = _super.call(this);

    _defineProperty(_assertThisInitialized(_this), "getData", void 0);

    _defineProperty(_assertThisInitialized(_this), "toNebulaFeature", void 0);

    _defineProperty(_assertThisInitialized(_this), "id", void 0);

    _defineProperty(_assertThisInitialized(_this), "helperLayers", void 0);

    _defineProperty(_assertThisInitialized(_this), "usesMapEvents", false);

    _defineProperty(_assertThisInitialized(_this), "enablePicking", false);

    _defineProperty(_assertThisInitialized(_this), "enableSelection", false);

    _this.id = (0, _uuid.v4)();
    _this.getData = getData;
    _this.toNebulaFeature = toNebulaFeature;
    _this.helperLayers = [];

    if (on) {
      // @ts-ignore
      Object.keys(on).forEach(function (key) {
        return _this.on(key, on[key]);
      });
    }

    return _this;
  }

  _createClass(NebulaLayer, [{
    key: "render",
    value: function render(config) {
      return null;
    }
  }]);

  return NebulaLayer;
}(_events["default"].EventEmitter);

exports["default"] = NebulaLayer;

The issue is then caused by this line:

_inherits(NebulaLayer, _Event$EventEmitter);

Indeed, executing the _inherits function with the given inputs throws the TypeError, because _Event$EventEmitter is not a function and is not null. Can you check why this is happening please?

I was able to fix this. Basically, vite externalized "events" on purpose (to allow browser compatibility). Therefore I couldn't use events.EventEmitter. To do so, I had to explicitly add the "events" library in my dependencies. For nebula.gl maintainers, this might be an issue also for create-react-app in the future, so I suggest you to also consider this aspect.