首页 > 解决方案 > 错误:模块没有自注册。(对于 onoff 包需要)

问题描述

我试图在他的节点 js 项目之一的 js 文件中要求包“onoff”。当我运行 js 文件时,出现如下错误

\node_modules\bindings\bindings.js:88 抛出 e ^

错误:模块没有自注册。

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)

请帮助解决这个问题。

提前致谢

帕拉维K

标签: node.jsnpmiot

解决方案


我也遇到了这个问题,最终嘲笑了本地开发的图书馆。多年来产生了一些问题,似乎作者要么没有要测试的 OSX,要么他对支持 OSX 不感兴趣。

与此问题相关的问题:

这是我的工作:

// 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;
    }
  }
};

实际的修复是create()只返回模拟类的方法。这允许我的客户端代码以相同的方式使用两者:

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

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

我没有使用库的完整 API,所以这个例子可能缺少一些细节。

更新:2018 年 12 月 15 日

我提交了一个 PR 以允许该accessible属性在 OSX 上工作,如文档中所述。希望它会被合并。

公关:https ://github.com/fivdi/onoff/pull/122


推荐阅读