node.js - 错误:模块没有自注册。(对于 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
解决方案
我也遇到了这个问题,最终嘲笑了本地开发的图书馆。多年来产生了一些问题,似乎作者要么没有要测试的 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,所以这个例子可能缺少一些细节。
更新:2018 年 12 月 15 日
我提交了一个 PR 以允许该accessible
属性在 OSX 上工作,如文档中所述。希望它会被合并。
推荐阅读
- microservices - 微服务学习或培训
- python - 无法在 pycharm 调试器中初始化应用引擎 SDK
- .net - 在 IIS 上对 oauth 的身份验证失败
- python - Keras LSTM 自动编码器:简单的顺序输入到顺序输出
- node.js - done 不是 passport.js 中的函数
- javascript - 如何将日期返回为 dd-FullMonth-yyyy
- sed - 如何使用新行进行 BSD sed 替换
- docker - 设置多个可以在没有 docker-compose 的情况下相互交互的服务
- mysql - 如何从mysql中选择json数组?
- python - 除了带有正则表达式的某些字符之外,如何删除字符串?