错误:模块未自行注册.(对于开关包要求)



我正在尝试在其中一个节点js项目上要求js文件中的包"onoff"。当我运行一个js文件时,出现如下错误

ode_modules\绑定\绑定.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_modulesbindingsbindings.js:81:44)
at Object.<anonymous> (node_modulesepollepoll.js:1:99)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)

请帮助解决这个问题。

提前致谢

帕拉维·

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

创建的问题与此问题相关:

  • https://github.com/fivdi/epoll/issues/12
  • https://github.com/fivdi/onoff/issues/69
  • https://github.com/fivdi/onoff/issues/106

这是我的解决方法:

// 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,因此此示例可能缺少一些详细信息。

更新日期: 12/15/2018

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

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

最新更新