Node.js: How to create ES6 module in C++

Viewed 241

This is the old example of creating a Node.js addon in C++:
https://nodejs.org/api/addons.html

It can be built with node-gyp to a common JS module, which works fine with function 'require'. However, the compiled module can't be imported in .mjs file.

This works fine: node test.js

// test.js
var m = require("./build/Release/addon");
console.log(m.hello());

This doesn't: node --experimental-modules test.mjs

// test.mjs
import * as m from "./build/Release/addon";
console.log(m.hello());

The error is: Error [ERR_MODULE_NOT_FOUND]: Cannot find module

That C++ addon example page is for the old Node.js with common JS, any examples for C++ addon that can be built to be ES6 module to import in .mjs files?

1 Answers
Related