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

115
Views
Is there a way to bypass the module-related code in a Node.js JavaScript file when loading it into a browser?

I have a JavaScript module that I want to use both in Node.js applications, and in applications over the Web. For Node.js uses I want to create a module object in the script and export it, so that it can be picked up in a require statement in another script file. Standard thing, e.g.:

module.exports = {
  foo: function () {
    // whatever
  },
  bar: function () {
    // whatever
  }
};

When I want to use this same script in a Web application, though, I of course use the <script> tag to import the file. However, the module code will then be flagged as unrecognized.

So my question is, is there some code I can bracket the module code with so that it gets skipped over in the browser? I wasn't a Web dev in the chaotic days of the browser wars, but I recall there being various workarounds to run different pieces of JS code depending on platform. So maybe there's something like that I can use here?

about 4 years ago · Juan Pablo Isaza
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!