# Importing a typescript module

**URL:** <https://talk.observablehq.com/t/importing-a-typescript-module/526>\
**Category:** Uncategorized\
**Created:** [March 20, 2018, 3:34am UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526 "2018-03-20T03:34:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaynox](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@jaynox](https://talk.observablehq.com/u/jaynox)\
**Post date:** [March 20, 2018, 3:34am UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/1 "2018-03-20T03:34:25Z")

</div>

I have been trying to use @creditkarma/thrift-parser@1.0.4 which is an npm package.json written in typescript, but I get the following errors:

import =\> Error: unable to load module

import with try/catch =\> TypeError: Failed to fetch dynamically imported module: [unpkg.com/@creditkarma/thrift-parser@1.0.4/dist/main/index.ts](http://unpkg.com/@creditkarma/thrift-parser@1.0.4/dist/main/index.ts)

wzrd.in =\> TypeError: Failed to fetch dynamically imported module: wzrd.in/standalone/@creditkarma/thrift-parser@1.0.4

require with try/catch: Error: invalid module  
at HTMLScriptElement.r.onload ([static.observableusercontent.com/worker/worker.84624bfaa8195ae8aae34c70e6a12fd2021308e0cce2ed522b3ef0a60a588a36.js:2:43374](http://static.observableusercontent.com/worker/worker.84624bfaa8195ae8aae34c70e6a12fd2021308e0cce2ed522b3ef0a60a588a36.js:2:43374))

---

<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:** [March 20, 2018, 5:41am UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/2 "2018-03-20T05:41:45Z")

</div>

Hi Jay,

Sure - so, like most TypeScript modules, the entry point for this one is just JavaScript - it includes extracted typings in a `.ts` file, but given that no JavaScript runtimes support TypeScript, it doesn’t point to TypeScript. That said, the file that its package.json points to uses CommonJS - you can decipher this y going to [https://unpkg.com/@creditkarma/thrift-parser@1.0.4/](https://unpkg.com/@creditkarma/thrift-parser@1.0.4/), checking the path in `package.json`, and then clicking to the file mentioned.

What I’ve found that works for this one is using bundle.run, though I’m pointing it to 1.0.3 rather than 1.0.4 - bundle.run had trouble bundling the newest revision:

```
require('https://bundle.run/@creditkarma/thrift-parser@1.0.3')

```

Perhaps by tomorrow bundle.run recovers from that instability and can bundle 1.0.4.

- Tom

---

<div class="post-metadata">

**Author:** ![jaynox](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@jaynox](https://talk.observablehq.com/u/jaynox)\
**Post date:** [March 20, 2018, 4:02pm UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/3 "2018-03-20T16:02:50Z")

</div>

Sweet! I’m not well versed in JS packaging, so all these magical wrapper sites are like secret incantations. I love how easy it is to learn with Observable – once you get a library loaded it is the quickest and easiest way to experiment.

Thanks for the quick solution!  
jay

---

<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:** [March 22, 2018, 7:13pm UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/4 "2018-03-22T19:13:07Z")

</div>

Minor update for any folks struggling with requiring packages: I just wrote a module require debugger that, for many modules, will quickly identify a working strategy:

> **[Module require debugger](https://observablehq.com/@tmcw/module-require-debugger)**
>
> An Observable notebook.

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [March 22, 2018, 7:22pm UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/5 "2018-03-22T19:22:39Z")

</div>

`propel` is all-frowny face

---

<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:** [March 22, 2018, 7:47pm UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/6 "2018-03-22T19:47:10Z")

</div>

That’s the hardest pattern! And the one I punted on for the initial notebook 😉

But I just implemented it - the notebook now identifies the ‘global leaks pattern’, and correctly guides you to a working strategy for propel, and for other modules that follow its pattern.

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [March 22, 2018, 8:11pm UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/7 "2018-03-22T20:11:22Z")

</div>

cool!. Looks like `require('https://unpkg.com/propel').catch(() => window.propel)` works too

> **[Hello Propel](https://observablehq.com/@fil/hello-propel)**
>
> An Observable notebook by Fil.

(the `plot` part still escapes me)  
[EDIT] propel.plot is half-solved here [https://beta.observablehq.com/@thadk/hack-propel-plot-to-work-in-observable](https://beta.observablehq.com/@thadk/hack-propel-plot-to-work-in-observable)

---

<div class="post-metadata">

**Author:** ![sdwfrost](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/sdwfrost/32/139_2.png) [@sdwfrost](https://talk.observablehq.com/u/sdwfrost)\
**Post date:** [March 22, 2018, 10:00pm UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/8 "2018-03-22T22:00:07Z")

</div>

Dear @tom

This is _so_ handy. When I tried it on `lib-r-math.js`, which says that it works with a global leak pattern, I just get undefined:

```auto
require('lib-r-math.js@1.0.75/dist/lib/libR.js').catch(() => window.libR)

```

This appears more to be a webpack issue when packaging for node, such that the bundle requires `tty` (which is node, not on npm) and `utils`. I re-packaged for web, and now I can just use a vanilla require:

[https://beta.observablehq.com/@sdwfrost/librmath](https://beta.observablehq.com/@sdwfrost/librmath)

---

<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:** [March 27, 2018, 7:55pm UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/9 "2018-03-27T19:55:15Z")

</div>

Ah, yeah - that’s another variant in which our exposing `require` actually handicaps the module requiring, due to how lib-r-math is packaged… will have to meditate on how to detect / import that style, and whether that style of bundled module will crop up in the future.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/jeremydouglass/32/672_2.png) [@jeremydouglass](https://talk.observablehq.com/u/jeremydouglass)\
**Post date:** [May 29, 2018, 7:46pm UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/10 "2018-05-29T19:46:14Z")

</div>

> [@tom](#):
>
> whether that style of bundled module will crop up in the future.

I am trying to require the [p5.dom library](https://p5js.org/reference/#/libraries/p5.dom) for p5.js – e.g. using the version on cloudflare. I tried the [module require debugger](https://beta.observablehq.com/@tmcw/module-require-debugger) notebook, which suggests:

```
require('p5@0.6.1/lib/addons/p5.dom.js')

```

…but this results in `undefined` in the Observable notebook. Adding `.catch() => .catch(() => window.p5dom` also results in `undefined`.

@tom – does this look the same as the `libR.js` issue above, or is this a different issue?

---

<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:** [May 29, 2018, 8:51pm UTC](https://talk.observablehq.com/t/importing-a-typescript-module/526/11 "2018-05-29T20:51:27Z")

</div>

That’s another issue - p5.dom doesn’t expose anything, it only augments the `p5` module. Here’s a notebook that requires it: [https://beta.observablehq.com/d/8f3f7067f7d32421](https://beta.observablehq.com/d/8f3f7067f7d32421) and the relevant code:

```auto
p5 = require('p5').then(async p5 => {
  await require('p5@0.6.1/lib/addons/p5.dom.js');
  return p5;
})

```
