🏠 back to Observable

Difficulty loading js function from raw Github


I’m trying to import https://raw.githubusercontent.com/nquinlan/better-random-numbers-for-javascript-mirror/master/support/js/MRG32k3a.js but Observable says “Unable to load module” and I see that my browser has an error:

Refused to execute script from ‘https://raw.githubusercontent.com/nquinlan/better-random-numbers-for-javascript-mirror/master/support/js/MRG32k3a.js’ because its MIME type (‘text/plain’) is not executable, and strict MIME type checking is enabled.

Is there a workaround for this?


Yes, there is! Use RawGit to fetch the file. RawGit will serve files with the appropriate content type, and for Chrome there’s even an extension to get the URL while surfing Github.


That said, using RawGit will only get you halfway in this instance - that file is just a function, with no export syntax. We support require() with AMD/UMD modules, and import() with ES6 modules. You can get around this by doing:

MRG32k3a = require("https://cdn.rawgit.com/nquinlan/better-random-numbers-for-javascript-mirror/master/support/js/MRG32k3a.js").catch(
  () => window.MRG32k3a

But ideally the better-random-numbers-for-javascript-mirror project supports any of UMD, AMD, CommonJS, or ES6 modules.


Mike has solved this problem once
Here is the reusable function which you can use

 async function requireFromGithub(jsFileUrl,prop){
  const response = await fetch(jsFileUrl);
  const blob = await response.blob();
  return require(URL.createObjectURL(blob)).catch(() => window[prop]);

You can use it like this



Thanks Tom, this is helpful.


I have created notebook which shows how to load different kind of data from some tricky, but commonly used endpoints

It covers, loading data from

  • Google sheets
  • Github
  • Cors restricted endpoint
  • From http endpoint (to https)
  • Excel file from Github
  • Scraping & Parsing
  • Requiring JS File from Github (like above question)

Hope it will be usable to someone