Import a module from github


I want to use the following module:, I tried

geo2rect = import("")

and got: SyntaxError: expected expression, got keyword 'import'

and also tried:

geo2rect = require("").catch(()=>window.geo2rect)

and got: undefined

Requiring scripts directly from is not possible - they set certain HTTP headers that prevent files from being loaded as scripts. You can fetch() other sorts of files and it’ll work, but not scripts, for GitHub’s security reasons.

But you can use RawGit in this case to load files from GitHub and RawGit is much more permissive. Here’s a require that works:

geo2rect = require('')

It’s possible, but it requires jumping through an extra hoop!

geo2rect = {
  const response = await fetch("");
  const blob = await response.blob();
  return require(URL.createObjectURL(blob)).catch(() => window.geo2rect);

Live demo:

(That said, it would be better to publish this library to npm, and require it from unpkg the normal way.)


Is there a way to do this for a class that uses imports/exports ?