Modules: using import for native module

Created on 15 Nov 2019  路  3Comments  路  Source: nodejs/modules

What is the plan for native code imports ?

import { x } from "./something.node";

Most helpful comment

For now, you can import native code into ESM via createRequire:

import { createRequire } from 'module';

const require = createRequire(import.meta.url);

const { x } = require('./something.node');

All 3 comments

I think they have similar issues to other parts of the require loader: Without running the code, it's not possible to tell what the exports are. So at most we'd support a default export.

Beyond that, IIRC there were multiple people concerned about adding support for native modules with proper WASM bindings just around the corner.

For now, the best way may be to set up a CJS wrapper that can interface with the native code.

For now, you can import native code into ESM via createRequire:

import { createRequire } from 'module';

const require = createRequire(import.meta.url);

const { x } = require('./something.node');

closing as it appears to have no movement and has a workaround, feel free to reopen

Was this page helpful?
0 / 5 - 0 ratings