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

289
Views
Using subtle in a Javascript module, from both the browser and NodeJS

NodeJS has an experimental implementation of the browser's crypto.subtle.

I have written a Javascript module which runs in both the browser and the NodeJS. It needs access to subtle.

For the moment, every function which needs to use subtle awaits the following function at the beginning of execution:

async function getSubtle() {
  if (typeof window === 'undefined') { // running in NodeJS
    const crypto = await import('crypto');
    return crypto.webcrypto.subtle;
  } else { // running in a browser
    return crypto.subtle;
  }
}

This works, but is slow and ugly as I'm re-importing the code on every function call (in NodeJS).

My project is simple enough that I do not use a build system. (I use express and symlink the modules I require, e.g. from public/js/modules/foo.mjs to lib/foo.js.)

Can I replace getSubtle() with a module-level non-async conditional import? If so, how?

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!