open-dis / open-dis-javascript

Javascript implementation of the IEEE-1278.1 Distributed Interactive Simulation (DIS) application protocol v6 and v7
BSD 2-Clause "Simplified" License
11 stars 11 forks source link

Importing open-dis on browser: [plugin:vite:import-analysis] Failed to resolve entry for package "open-dis". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "open-dis". The package may have incorrect main/module/exports specified in its package.json. #12

Closed NathanielPoteat closed 1 year ago

NathanielPoteat commented 1 year ago

I'm trying to import open-dis into a project that I'm working on to be able to package Pdus clientside, but keep getting the error message in the title when importing as import open-dis;.

When importing as const dis = require("open-dis"); I instead get an error message that that states: Uncaught ReferenceError: require is not defined.

Otherwise, I can import import { Pdu } from 'open-dis/src/dis/Pdu'. However, in this case, I get an error message in the browser stating: ` Uncaught ReferenceError: assignment to undeclared variable dis js SignalPdu.js:11 __require2 chunk-ASBRWZGP.js:15

open-dis_src_dis_SignalPdu:1 `
leif81 commented 1 year ago

There's an example provided by another user in the readme. Worth a look if you haven't yet: https://github.com/keckxde/node-disnetworkclient

NathanielPoteat commented 1 year ago

There's an example provided by another user in the readme. Worth a look if you haven't yet: https://github.com/keckxde/node-disnetworkclient

I have already looked into how open-dis is imported there. I believe that the issue lies in trying to import open-dis into a browser rather than NodeJS. I'll probably either have to create a fork and convert the imports to the ES6 syntax or just take the hopefully easy way and use requirejs or browserify.

leif81 commented 1 year ago

Ok I am not familiar with running it directly in a browser. If you get it working and don't mind sharing your results I'm curious how it turns out.

NathanielPoteat commented 1 year ago

Trying to import open-dis using a browserify and requirejs didn't work, so I looked into making some changes to the source code to implement the JavaScript ES6 import method instead. That got me closer, but it now throws a was blocked because of a disallowed MIME type (“”). I'm going to keep looking into it, but it doesn't seem like it will be a quick fix to get open-dis to import to and work on the browser.

NathanielPoteat commented 1 year ago

Ok I am not familiar with running it directly in a browser. If you get it working and don't mind sharing your results I'm curious how it turns out.

I ended up creating a fork and swapped out the nodejs import/export syntax for the es6 import/export syntax and it works in the browser now. You can check out the fork repository here at https://github.com/NathanielPoteat/open-dis-ts-es6/tree/ts-es6-conversions to see the changes I made to make it work. As an aside and a matter of preference, I also took the liberty of converting the files in my fork to TypeScript and created a min file containing only the files I needed for the project I'm working on.

leif81 commented 1 year ago

Glad to hear it worked out.