terasum / js-mdict

*.mdx/*.mdd interpreter js implements, support mdict index file
MIT License
159 stars 18 forks source link

Mdict is not a constructor? #76

Closed mshtang closed 1 year ago

mshtang commented 1 year ago

Hi, I am trying out this module but got a TypeError: Mdict is not a constructor.

I tested it this way: create a main.js file, npm install js-mdict, add the following lines to the script file and run it in the console by node main.js. I tried with node v17.3 and v18.15 but neither worked out for me.

import Mdict from 'js-mdict';
const dict = new Mdict('./testDict/data.mdx');  // <= error
console.log('lookup red', dict.lookup('red'));

Am I using the module correctly?

Thank you for your help!

terasum commented 1 year ago

you cannot use import Mdict from 'js-mdict' directly, you should do this:

const Mdict = require('js-mdict');
const dict = new Mdict.default('./testDict/data.mdx');  <= you should use Mdict.default;
console.log('lookup red', dict.lookup('red'));

or you can use babel or webpack to compile es6 to commonjs syntax to run at bare node environment

mshtang commented 1 year ago

Thank you for the speedy response. Using new Mdict.default(...) solves the problem.