You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Maybe you should try setting the command-line options --experimental-specifier-resolution = node. The .node extension that isn't supported default has been a legacy extension. you should add the above options to support this extension.
@Brooooooklyn I don't think this is specific to Node-API, more related to ESM. @nodejs/modules has there been discussion around the ESM plans for loading native modules?
, I forget the exact reasoning but it partially dealt with not having a general MIME agreed upon / some push back since it would have people write code that didn't work in other environments / the introduction of createRequire.
Supporting it via import is probably something we can’t do just yet until we wait and see how the spec settles with regard to JSON and WebAssembly imports. For now I think you should be able to do:
As I metioned below, createRequire usage is too verbose for library authors. For example if I had 10 functions exported from native addon and I want provide esm package for these functions, I need to write these codes:
As I metioned below, createRequire usage is too verbose for library authors. For example if I had 10 functions exported from native addon and I want provide esm package for these functions, I need to write these codes:
Something worth noting is that if you do go with the createRequire(import.meta.url), you might end up breaking bundling as the expression is no longer analyzable. The nice part about cjs is that once require is detected, most bundlers will have a way to copy the binary to your dist folder and change the import path, so everything just works.
If you do decide to go via createRequire route, you risk bailing out of that detection and end up with either build errors or runtime errors (missing bindings as they were never moved to the correct path). As a library author, I would want to see .node file imports being directly supported without us requiring to add any global helpers that risk naming collision or subtly breaking out of the build process.
@JonasBa I made the change in my application and seems there are no problems at least on Windows. Now I'm waiting for the test on Linux. Also Linux has no issues.
Activity
iam-frankqiu commentedon Oct 21, 2021
Maybe you should try setting the command-line options
--experimental-specifier-resolution = node. The.nodeextension that isn't supported default has been a legacy extension. you should add the above options to support this extension.Brooooooklyn commentedon Oct 21, 2021
Should we default support
.nodefile?iam-frankqiu commentedon Oct 21, 2021
I don't think it's a good idea. actually. I even don't think
.mjsshould support default. I like the design of simple and easy to use.Brooooooklyn commentedon Oct 21, 2021
What's the recommend way to deliver native addon as esm package?
Brooooooklyn commentedon Oct 21, 2021
I need to wrap some annoying codes to make my package could be imported in esm:
iam-frankqiu commentedon Oct 21, 2021
https://nodejs.org/api/module.html#module_module_createrequire_filename
Maybe it can help you to solve the puzzle.
Brooooooklyn commentedon Oct 21, 2021
It doesn't help at all.
It turns codes into:
Please reopen this issue.
Brooooooklyn commentedon Oct 25, 2021
@mhdawson is this issue belong to
Node-APIscope?mhdawson commentedon Oct 25, 2021
@Brooooooklyn I don't think this is specific to Node-API, more related to ESM. @nodejs/modules has there been discussion around the ESM plans for loading native modules?
bmeck commentedon Oct 25, 2021
At some point in time it was ripped out and moved to
node/lib/internal/modules/esm/get_format.js
Line 26 in df22736
createRequire.GeoffreyBooth commentedon Oct 25, 2021
Supporting it via
importis probably something we can’t do just yet until we wait and see how the spec settles with regard to JSON and WebAssembly imports. For now I think you should be able to do:See https://nodejs.org/api/module.html#modulecreaterequirefilename
Brooooooklyn commentedon Oct 25, 2021
As I metioned below,
createRequireusage is too verbose for library authors. For example if I had 10 functions exported from native addon and I want provide esm package for these functions, I need to write these codes:8 remaining items
motla commentedon Dec 20, 2021
@Brooooooklyn What about, in your module
my-native-addon:main.mjs
Then in your application code
or
Brooooooklyn commentedon Dec 20, 2021
@motla I want my package to be compatible with CommonJS and esm. And I don't want to write two copies of index.js
fix named exports
[keyserver] Fix rust-node-addon after deprecation of --experimental-s…
JonasBa commentedon Jun 16, 2023
Something worth noting is that if you do go with the createRequire(import.meta.url), you might end up breaking bundling as the expression is no longer analyzable. The nice part about cjs is that once require is detected, most bundlers will have a way to copy the binary to your dist folder and change the import path, so everything just works.
If you do decide to go via createRequire route, you risk bailing out of that detection and end up with either build errors or runtime errors (missing bindings as they were never moved to the correct path). As a library author, I would want to see .node file imports being directly supported without us requiring to add any global helpers that risk naming collision or subtly breaking out of the build process.
d3x0r commentedon Aug 30, 2023
How is not supporting
.nodein .mjs import making this 'easy to use' ?TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".node" for (fullpathto)\sack_vfs.nodeokay - fine I'll use require....
ReferenceError: require is not defined in ES module scope, you can use import instead(no I can't.)and then early suggestions of
--experimental-specifier-resolution=nodeis deprecated.GeoffreyBooth commentedon Aug 30, 2023
d3x0r commentedon Aug 30, 2023
file1.cs
fileUse.mjs
Still 2 lines - just 1 in an extra file.
guybedford commentedon Aug 31, 2023
Personally, I also continue to be surprised no one ever landed this.
crystalfp commentedon Nov 11, 2024
@JonasBa I made the change in my application and seems there are no problems at least on Windows. Now I'm waiting for the test on Linux. Also Linux has no issues.
esm: add experimental support for addon modules
esm: add experimental support for addon modules
esm: add experimental support for addon modules