我面临一个非常简单的问题。我想在nodejs的JavaScript脚本中使用名为Rbush
的pakcage。我已经使用npm install rbush
命令安装了它,当我尝试使用它时会出现此错误
ReferenceError: RBush is not defined
这是我的代码
const t=require('rbush');
const tree = new RBush();
const item = {
minX: 20,
minY: 40,
maxX: 30,
maxY: 50,
foo: 'bar'
};
tree.insert(item);
[我知道我必须将其包含在脚本中,并且已经使用require
函数完成了此操作,但是似乎它没有在其中添加库。我也尝试了以下方法,但仍然无法使用。
const tree=require('rbush');
const item = {
minX: 20,
minY: 40,
maxX: 30,
maxY: 50,
foo: 'bar'
};
tree.insert(item);
节点模块导出不同的类型,因此,当您require
模块时,检查文档并查看获得的内容很重要。在这方面,rbush
的用法文档不是很好,但是它导出了一个类,因此您的第一个示例几乎是正确的。
分配模块的变量就是您需要在代码中引用它的方式。因此,您可能不想将require('rbush')
分配给t
,而是将其分配给RBush
。 (您可以随意命名,但如果要调用new RBush()
,则需要使用该名称,const RBush = require('rbush');
const RBush = require('rbush');
const tree = new RBush();
const item = {
minX: 20,
minY: 40,
maxX: 30,
maxY: 50,
foo: 'bar'
};
tree.insert(item);