Wulf / nodehun

The Hunspell binding for NodeJS that exposes as much of Hunspell as possible and also adds new features. Hunspell is a first class spellcheck library used by Google, Apple, and Mozilla.
MIT License
287 stars 42 forks source link

Does not work from ESM #104

Open wooorm opened 2 years ago

wooorm commented 2 years ago

Seems that the .node extension trips up Node when running a file as a module (ESM).

To repro on node 12, 14, 16, 17:

mkdir example
cd example
npm init -y
touch example.mjs

in example.mjs:

import {Nodehun} from 'nodehun'

console.log(Nodehun)

Then running node example.js yields:

$ node example.mjs 
node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".node" for /Users/tilde/.../example/node_modules/nodehun/build/Release/Nodehun.node
    at new NodeError (node:internal/errors:371:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:87:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:102:38)
    at defaultLoad (node:internal/modules/esm/load:21:14)
    at ESMLoader.load (node:internal/modules/esm/loader:359:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:280:58)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:297:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:261:34)
    at async ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:81:21) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Node.js v17.4.0
Wulf commented 2 years ago

Thanks for reporting this @wooorm :) Sorry for the late response -- I looked into this, and apparently, it's just not supported currently:

image

Here's a snippet for those who are trying to use nodehun with ESM:

import { createRequire } from 'module'
const require = createRequire(import.meta.url)

const Nodehun = require('nodehun')

I'm going to pin this so others can see it :)

Side note: it's cool to see your username pop up from time to time! :)