【发布时间】:2019-08-21 15:17:38
【问题描述】:
我正在尝试使用 Fuse.js 库并使用 Node.js 通过终端显示它的结果,但是,我似乎找不到将外部库合并到我的代码中的方法。
我用谷歌搜索了一下,发现我需要添加 var foo = require("MyLibraryPath.js");一开始,但在尝试之后我仍然无法访问该库中的任何功能。
我还尝试将整个库复制并粘贴到代码的开头,但它会引发错误。
到目前为止,我的代码看起来像这样。该库用于搜索数组并将最相似的项目返回到您在数组中进行的搜索。我过度简化它只是为了让你可以看到它停止工作的地方
var getFuse = require("fuse.js");
var fs = require("fs");
var arr = [
{item:"one"},
{item:"two"}
];
var options = {
//insert search options here
}
//In order to search, I need to put things like this
var fuse = new Fuse( array ,options);
//Here I insert what I want to search
var result = fuse.search("one");
但是,当我打开终端并执行时
node myFile.js
我明白了
ReferenceError: Fuse is not defined`
at Object.<anonymous> (/Users/myFile.js:21:12)
at Module._compile (internal/modules/cjs/loader.js:707:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
at Module.load (internal/modules/cjs/loader.js:605:32)
at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
at Function.Module._load (internal/modules/cjs/loader.js:536:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:760:12)
at startup (internal/bootstrap/node.js:308:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:878:3)
我是否错误地调用了库?是否无法加载外部库,我尝试使用外部链接和下载库并在本地调用它,但它给了我同样的错误。
【问题讨论】:
-
您在
require()行中将变量命名为getFuse,而不是Fuse。 -
var fuse = new getFuse( array ,options); -
你不声明那个命名变量,你需要先声明然后使用它。
-
谢谢大家!我是 node.js 的新手,所以我仍然容易犯这些愚蠢的错误。现在可以使用了!
标签: javascript node.js terminal fuse.js