Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work from ESM #104

Open
wooorm opened this issue Apr 12, 2022 · 1 comment
Open

Does not work from ESM #104

wooorm opened this issue Apr 12, 2022 · 1 comment

Comments

@wooorm
Copy link
Collaborator

wooorm commented Apr 12, 2022

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
Copy link
Owner

Wulf commented Jul 18, 2022

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! :)

@Wulf Wulf pinned this issue Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants