# Import a module from github

**URL:** <https://talk.observablehq.com/t/import-a-module-from-github/605>\
**Category:** Help\
**Created:** [April 5, 2018, 8:33pm UTC](https://talk.observablehq.com/t/import-a-module-from-github/605 "2018-04-05T20:33:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![severo](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/severo/32/786_2.png) [@severo](https://talk.observablehq.com/u/severo)\
**Post date:** [April 5, 2018, 8:33pm UTC](https://talk.observablehq.com/t/import-a-module-from-github/605/1 "2018-04-05T20:33:20Z")

</div>

Hi,

I want to use the following module: [https://github.com/sebastian-meier/d3.geo2rect/blob/master/build/geo2rect.min.js](https://github.com/sebastian-meier/d3.geo2rect/blob/master/build/geo2rect.min.js), I tried [https://talk.observablehq.com/t/load-require-import-from-github-pages/296:](https://talk.observablehq.com/t/load-require-import-from-github-pages/296:)

```
geo2rect = import("https://raw.githubusercontent.com/sebastian-meier/d3.geo2rect/master/build/geo2rect.min.js")

```

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

and also tried:

```
geo2rect = require("https://raw.githubusercontent.com/sebastian-meier/d3.geo2rect/master/build/geo2rect.min.js").catch(()=>window.geo2rect)

```

and got: `undefined`

---

<div class="post-metadata">

**Author:** ![tom](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tom/32/2262_2.png) [@tom](https://talk.observablehq.com/u/tom)\
**Post date:** [April 5, 2018, 8:40pm UTC](https://talk.observablehq.com/t/import-a-module-from-github/605/2 "2018-04-05T20:40:16Z")

</div>

Requiring _scripts_ directly from [githubusercontent.com](http://githubusercontent.com) 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](https://rawgit.com/) in this case to load files from GitHub and RawGit is much more permissive. Here’s a `require` that works:

```auto
geo2rect = require('https://rawgit.com/sebastian-meier/d3.geo2rect/master/build/geo2rect.js')

```

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [April 5, 2018, 11:12pm UTC](https://talk.observablehq.com/t/import-a-module-from-github/605/3 "2018-04-05T23:12:24Z")

</div>

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

```auto
geo2rect = {
  const response = await fetch("https://raw.githubusercontent.com/sebastian-meier/d3.geo2rect/master/build/geo2rect.min.js");
  const blob = await response.blob();
  return require(URL.createObjectURL(blob)).catch(() => window.geo2rect);
}

```

Live demo: [https://beta.observablehq.com/@mbostock/require-from-github](https://beta.observablehq.com/@mbostock/require-from-github)

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

---

<div class="post-metadata">

**Author:** ![dhowe](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/dhowe/32/4149_2.png) [@dhowe](https://talk.observablehq.com/u/dhowe)\
**Post date:** [December 28, 2021, 4:03am UTC](https://talk.observablehq.com/t/import-a-module-from-github/605/4 "2021-12-28T04:03:30Z")

</div>

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