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

chore: Add some hello world examples #205

Merged
merged 1 commit into from
Dec 8, 2023

Conversation

GordonSmith
Copy link
Member

Fixes #203

@rubenlg
Copy link

rubenlg commented Dec 8, 2023

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

@rubenlg
Copy link

rubenlg commented Dec 8, 2023

To follow up, I upgraded nodejs to v20 and got the same error.

@GordonSmith GordonSmith force-pushed the GH-203-EXAMPLES branch 2 times, most recently from 9061239 to 1f9bff4 Compare December 8, 2023 10:59
@GordonSmith
Copy link
Member Author

GordonSmith commented Dec 8, 2023

@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();

@rubenlg
Copy link

rubenlg commented Dec 8, 2023

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:

npm install --save-dev @types/node

I'll test my project and let you know, but this is very promising!

@rubenlg
Copy link

rubenlg commented Dec 8, 2023

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

@GordonSmith
Copy link
Member Author

GordonSmith commented Dec 8, 2023

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();

@rubenlg
Copy link

rubenlg commented Dec 8, 2023

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>
@GordonSmith
Copy link
Member Author

...nice workaround for Electron...

@GordonSmith GordonSmith merged commit da81597 into hpcc-systems:trunk Dec 8, 2023
5 checks passed
@GordonSmith GordonSmith deleted the GH-203-EXAMPLES branch December 8, 2023 11:58
@rubenlg
Copy link

rubenlg commented Dec 8, 2023

Thanks a lot @GordonSmith !!

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

Successfully merging this pull request may close these issues.

2.15 not compiling with Typescript
2 participants