Importing lunr in typescript?

1.2k views Asked by At

The Defintely Typed repository imports it like this:

import * as lunr from 'lunr';

However when using it like that in Stackblitz it errors with:

lunr is not a function

Thoughts? This is the Stackblitz:

https://stackblitz.com/edit/typescript-lunr-test

2

There are 2 answers

1
JBallin On BEST ANSWER

Definitely Typed is using an older version of lunr, so I tried a different import: import lunr from "lunr", which throws an error saying posting[field] is undefined (source code).

I then noticed that the "text" field being used in the Builder was not initialized in the index. Adding this.field("text") in the index resulted in the page loading without errors.

2
user549442 On

Try using:

import lunr from "lunr"

This way works because lunr.js is exporting a default or its overriding the export objects with a singular thing. Take a look at the first 4-5 paragraphs of this article: blog.atomist.com/typescript-imports