Node.js 如何在脚本中包含nodejs包

Node.js 如何在脚本中包含nodejs包,node.js,Node.js,我面临一个非常简单的问题。我想在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,

我面临一个非常简单的问题。我想在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);


节点模块导出不同的类型,因此当您
需要
模块时,检查文档并查看“得到”的内容非常重要。在这方面,
rbush
的用法文档不是很好,但它导出了一个类,因此您的第一个示例几乎是正确的

分配模块的变量是您需要如何在代码中引用它。因此,与其将
require('rbush')
分配给
t
,不如将其分配给
rbush
。(您可以随意命名它,但如果要调用
new RBush()
,则需要时需要使用该名称,
const RBush=require('RBush');


这是其中一个比询问Stackoverflow对世界其他地方更有用的案例。当然,除非你在这里得到答案后准备对rbush提交PR。@Mike'Pomax'Kamermans谢谢Mike,我想问这个问题,但我认为添加nodejs包是一个更一般的问题
const RBush = require('rbush');

const tree = new RBush();
const item = {
  minX: 20,
  minY: 40,
  maxX: 30,
  maxY: 50,
  foo: 'bar'
};
tree.insert(item);