0
votes

I am trying to require the package "onoff" in js file on one of he node js project. When i run a js file i get error as below

\node_modules\bindings\bindings.js:88 throw e ^

Error: Module did not self-register.

at Object.Module._extensions..node (module.js:670:18)

at Module.load (module.js:560:32)

at tryModuleLoad (module.js:503:12)

at Function.Module._load (module.js:495:3)

at Module.require (module.js:585:17)

at require (internal/module.js:11:18)

at bindings (\node_modules\bindings\bindings.js:81:44)

at Object.<anonymous> (\node_modules\epoll\epoll.js:1:99)

at Module._compile (module.js:641:30)

at Object.Module._extensions..js (module.js:652:10)

Please help through this.

Thanks in advance

Pallavi K

1
Did you got any solution ? - Yogeshwar Tanwar

1 Answers

1
votes

I've run into this issue as well and ended up mocking the library for local development. There has been a few issues created over the years, and it seems like the author either doesn't have OSX to test or he just isn't interested in supporting OSX in general.

Issues created related to this problem:

This is the work around I have:

// GpioFactory.js
class MockGPIO {
  constructor(pin, direction) {
    this._value = 0;
    this._direction = direction;
  }

  readSync() { return this._value; }
  read(cb) { cb(null, this._value) }
  writeSync(value) { this._value = value }
  write(value, cb) {
    this._value = value;
    cb(null, value);
  }
  watch(cb) {}
  unwatch(cb) {}
  unwatchAll() {}
  direction() { return this._direction }
  setDirection(direction) { this._direction = direction}
  edge() { return 0; }
  setEdge(edge) {}
  activeLow() { return true; }
  setActiveLow(invert) {}
  unexport() {}
}

MockGPIO.accessible = false;
MockGPIO.HIGH = 1;
MockGPIO.LOW = 0;

module.exports = {
  create: () => {
    try {
      return require('onoff').Gpio;
    } catch (e) {
      console.error('Using mock Gpio');
      return MockGPIO;
    }
  }
};

The actual fix is the create() method that just returns the mock class. This allows my client code to use both the same way:

const GpioFactory = require('./GpioFactory');
const Gpio = GpioFactory.create();

const garageButton = new Gpio(4, 'out');

I don't use the full API of the library, so this example is likely missing some details.

Update: 12/15/2018

I submitted a PR to allow the accessible property to work on OSX as described in the docs. Hopefully it'll get merged.

PR: https://github.com/fivdi/onoff/pull/122