Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
JavaScript - cannot find imported file

I'm trying to create a JavaScript worker, where I require the JSONfn library. I'm not entirely sure how to import it?

<html>
    <script type="text/javascript" src="jsonfn.js"></script>

    <script id="worker" type="javascript/worker">
        self.onmessage = function f(message){
              console.log(JSONfn.parse(message...)
        }
    ...
    </script>
<html>

I get the following error:

Uncaught ReferenceError: JSONfn is not defined
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To import external scripts in your Web Worker, you can use the importScripts() method available in non-module Worker contexts.

However, this particular library doesn't seem to be meant for use in a Worker (it calls the window getter, which isn't available in Worker contexts). It seems that simply redirecting window to self does the trick, but it may very well fail in other places.

Also, to parse back from the main thread you'll obviously have to load the library again in that context (through a <script>).

const worker_url = URL.createObjectURL(new Blob([document.querySelector("[type=worker]").textContent], {type: "text/javascript"}));
const worker = new Worker(worker_url);
worker.onmessage = (evt) => {
  console.log("received", evt.data);
  const parsed = JSONfn.parse(evt.data);
  console.log(parsed)
  console.log(parsed.baz());
};
<script type="worker">
window = self; // jsonfn uses "window"
importScripts("https://cdn.jsdelivr.net/gh/vkiryukhin/jsonfn/jsonfn.js");
const obj = {foo: "bar", baz: function() { return "blah"; }}
const json = JSONfn.stringify(obj);
postMessage(json);
</script>
<!-- to parse from the main thread we need to load the library here too -->
<script src="https://cdn.jsdelivr.net/gh/vkiryukhin/jsonfn/jsonfn.js"></script>

Also beware that this library requires that you do declare your function with the function keyword (arrow functions or method syntax aren't supported), and that it won't be able to retrieve anything outside of the scope of the function, it really just does copy the source code, nothing about the context.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!