0

I'm trying to use the Fuse.js library and display it's result through the terminal using Node.js, however, I can't seem to find a way to incorporate external libraries into my code.

I googled about it for a bit, and found that I need to add var foo = require("MyLibraryPath.js"); at the beginning, but after trying that I can't still access any function from that library.

I also tried to just copy and paste the whole library at the beginning of my code, but it throws an error.

My code so far looks something like this. This library is used to search through arrays and return the most similar items to the search you did in your array. I oversimplified it just so you can see where it stops working

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");

However, when I open the terminal and execute

node myFile.js

I get

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)

Am I calling the library incorrectly? Is it not possible to load external libraries, I tried both using the external link and downloading the library and calling it locally, but it gave me the same error.

4
  • 1
    You named the variable getFuse, not Fuse, in your require() line. Commented Aug 21, 2019 at 15:20
  • var fuse = new getFuse( array ,options); Commented Aug 21, 2019 at 15:20
  • You don't declare that named variable, you need to declare it first and then use it. Commented Aug 21, 2019 at 15:20
  • Thanks guys! I'm new to node.js so I'm still prone to these kind of silly mistakes. It works now! Commented Aug 21, 2019 at 15:47

2 Answers 2

2

Instead of var getFuse = require("fuse.js");

Use var Fuse = require("fuse.js");

You are using a variable that doesn't yet exist which is why you get that error.

Sign up to request clarification or add additional context in comments.

Comments

1

You're calling Fuse() but you never define it.

Here — var getFuse = require("fuse.js"); — you define getFuse (by assigning whatever the module exports) but you never do anything with that.

Perhaps you meant var Fuse = require("fuse.js");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.