Uncaught ReferenceError: exports is not defined

1,006 views
Skip to first unread message

R K

unread,
Feb 28, 2022, 3:43:38 AM2/28/22
to FlatBuffers
I am trying to migrate to flatbuffers-2.0 and am running into this. I am using javascript on client side ( browser )  and sending flatbuffers to C++ server. All fine except , the client stopped working with error "Uncaught ReferenceError: exports is not defined"
The problematic statement is "Object.defineProperty(exports, "__esModule", { value: true });"
I am using typescript compiler ( tsc ) to compile typescript emitted by flatc compiler. 
my command is like  "tsc --target ES5 --module amd  --outFile peer_generated.js *.ts"
if i give any thing other than "amd" or "system" for module option  , things dont work. 
A google search leads to confusing solutions which are mostly nodejs based.
Thanks & regards,
-rk  

mikkelfj

unread,
Feb 28, 2022, 5:08:37 PM2/28/22
to FlatBuffers
I think I've heard of this before - if the error is after the tsc compilation. In a few versions back, JS FlatBuffers lib was Javascript, not typescript. The typescript version uses modules and this changes have you can place the code in a web page. Therefore you cannot just compile the typescript code and expect it to work, but it can be made to work with a few tweaks. Unfortunately I have not done this myself, so you will have to look at some examples etc. I am not able to dig in further atm.

On a related note: tsc pulls in a few dependencies that you might not want. I just learned about esbuild - a Go application that is a fast bundler, but should also be able to translate typescript (though not validate it). If anyone has tried using that in place of tsc, I'd be curious to know, and exactly how.
Reply all
Reply to author
Forward
0 new messages