lucaong / minisearch

Tiny and powerful JavaScript full-text search engine for browser and Node
https://lucaong.github.io/minisearch/
MIT License
4.67k stars 133 forks source link

Calling the MiniSearch constructor causes typescript error `TypeError: minisearch_1.default is not a constructor` #202

Closed leopoldodonnell closed 1 year ago

leopoldodonnell commented 1 year ago

I've seen folks have this similar issue, but it persists and isn't clear how to get it working:

In package.json

 "dependencies": {
    "minisearch": "^6.0.1",
    "tslint": "^6.1.3",
    "typescript": "^4.9.5"
  },

Code in indes.ts

import MiniSearch from 'minisearch';

class User {
  userId = '';
  firstName: string = '';
  lastName: string = '';
  email: string = '';
  city: string = '';
}

const userCache = new MiniSearch<User>({ fields: ["userId", "email", "city"] });

Error:

ts-node src/index.ts 
/Users/leopoldodonnell/dev/arch/mini-search/src/index.ts:12
const userCache = new MiniSearch<User>({ fields: ["userId", "email", "city"] });
                  ^
TypeError: minisearch_1.default is not a constructor
    at Object.<anonymous> (/Users/xxxxxxxxx/mini-search/src/index.ts:12:19)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module.m._compile (/Users/xxxxxxx/.nvm/versions/node/v16.16.0/lib/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/xxxxxxx/.nvm/versions/node/v16.16.0/lib/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at phase4 (/Users/xxxxxxx/.nvm/versions/node/v16.16.0/lib/node_modules/ts-node/src/bin.ts:649:14)
    at bootstrap (/Users/xxxxxxx/.nvm/versions/node/v16.16.0/lib/node_modules/ts-node/src/bin.ts:95:10)

This could be my environment or something subtile in the module - either way I'm hoping someone will have seen and addressed this.

lucaong commented 1 year ago

Hi @leopoldodonnell , I see you are using TypeScript, did you enable in your .tsconfig.json the option esModuleInterop ?

leopoldodonnell commented 1 year ago

Thanks @lucaong - I had added and removed it after following various threads - this addresses the issue

lucaong commented 1 year ago

Happy that it helped :)