-
Notifications
You must be signed in to change notification settings - Fork 21
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
chore: Add some hello world examples #205
Conversation
I tried the commonJS version, and here is the output: npm install # OK
npm run build # OK
node index.js
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/hpcc-js-wasm/examples/typescript/hello-world/commonjs/node_modules/@hpcc-js/wasm/dist/index.umd.js from /tmp/hpcc-js-wasm/examples/typescript/hello-world/commonjs/index.js not supported.
index.umd.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.umd.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /tmp/hpcc-js-wasm/examples/typescript/hello-world/commonjs/node_modules/@hpcc-js/wasm/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
at /tmp/hpcc-js-wasm/examples/typescript/hello-world/commonjs/index.js:36:78 {
code: 'ERR_REQUIRE_ESM'
}
Node.js v18.17.1 |
To follow up, I upgraded nodejs to v20 and got the same error. |
9061239
to
1f9bff4
Compare
@rubenlg - I just released v2.15.1 and I believe the following code should work as expected in TS + CommonJS: const { Graphviz } = require("@hpcc-js/wasm/graphviz");
async function render() {
const graphviz = await Graphviz.load();
console.log(graphviz.dot('digraph G { Hello -> World }'));
}
render(); |
Now the CommonJS example in this PR works. I had to install the nodejs types on top of what's included in the package.json:
I'll test my project and let you know, but this is very promising! |
It's working! Typings get erased by using require, but it works, and my code using graphviz isn't that complex that losing types is a big deal. Thanks!! |
FYI The following should also work (and keep typings): import { Graphviz } from "@hpcc-js/wasm";
async function render() {
const graphviz = await Graphviz.load();
console.log(graphviz.dot('digraph G { Hello -> World }'));
}
render(); |
The second approach for some reason crashes Electron. I couldn't find why, even enabling debugging flags. However, I found this workaround works and has correct types: import type {Graphviz as GraphvizNS} from '@hpcc-js/wasm';
const Graphviz: typeof GraphvizNS = require("@hpcc-js/wasm/graphviz").Graphviz; |
Fixes hpcc-systems#203 Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
1f9bff4
to
56058b8
Compare
...nice workaround for Electron... |
Thanks a lot @GordonSmith !! |
Fixes #203