# TypeError: Convert is not a constructor

**URL:** <https://talk.observablehq.com/t/typeerror-convert-is-not-a-constructor/3657>\
**Category:** Help\
**Created:** [July 30, 2020, 9:15am UTC](https://talk.observablehq.com/t/typeerror-convert-is-not-a-constructor/3657 "2020-07-30T09:15:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![abitrolly](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/abitrolly/32/7214_2.png) [@abitrolly](https://talk.observablehq.com/u/abitrolly)\
**Post date:** [July 30, 2020, 9:15am UTC](https://talk.observablehq.com/t/typeerror-convert-is-not-a-constructor/3657/1 "2020-07-30T09:15:14Z")

</div>

I was trying to use the standard example from `ansi-to-html` `npm` packae.

```auto
var Convert = require('ansi-to-html');
var convert = new Convert();

console.log(convert.toHtml('\x1b[30mblack\x1b[37mwhite'));

```

And while trying to wrap the example into an Observable Cell, I’ve got the `TypeError: Convert is not a constructor`. The Cell.

```auto
ansitohtml = {
  let Convert = require('https://bundle.run/ansi-to-html@0.6.14');
  let convert = new Convert();
  return convert
}

```

The Error.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/6/66910730a5697036c1b76cf058a2a5e0b8f74a0d.png)

The error here happens because Observable `require` is asynchronous ([returns a promise](https://observablehq.com/@observablehq/five-minute-introduction)) and constructors in JavaScript are not - `new` operation doesn’t understand what a promise is. The solution is to `await` until the promise resolves.

```auto
ansitohtml = {
  let Convert = await require('https://bundle.run/ansi-to-html@0.6.14');
  let convert = new Convert();
  return convert
}

ansitohtml.toHtml('\x1b[94mRunning number: \x1b[96mtest_line\e[0m')

```

Or maybe [Observable language](https://observablehq.com/@observablehq/observables-not-javascript) can make `new` operation understand asynchronous objects as well?

---

<div class="post-metadata">

**Author:** ![aaronkyle](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/aaronkyle/32/8106_2.png) [@aaronkyle](https://talk.observablehq.com/u/aaronkyle)\
**Post date:** [July 30, 2020, 11:18am UTC](https://talk.observablehq.com/t/typeerror-convert-is-not-a-constructor/3657/2 "2020-07-30T11:18:21Z")

</div>

Hi @abitrolly,

I am not sure if this will respond to your question in any way, but I tried re-writing the ‘standard example’ in more of an OberservableHQ manner, and it seems to work just fine:

> **[ansitohtml](https://observablehq.com/d/66fb383f7bfe786c)**
>
> An Observable notebook by Aaron Kyle Dennis.

Essentially, I just used the Observable ‘require’ and followed along the variable names.

If this wasn’t it, I hope others will respond.

Wishing you the very best,

Aaron

---

<div class="post-metadata">

**Author:** ![abitrolly](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/abitrolly/32/7214_2.png) [@abitrolly](https://talk.observablehq.com/u/abitrolly)\
**Post date:** [July 30, 2020, 1:01pm UTC](https://talk.observablehq.com/t/typeerror-convert-is-not-a-constructor/3657/3 "2020-07-30T13:01:40Z")

</div>

This works, because `require` and `new` are located in separate cells. I tried to set `ansitohtml` function in a single cell to keep notebook simple.

---

<div class="post-metadata">

**Author:** ![abitrolly](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/abitrolly/32/7214_2.png) [@abitrolly](https://talk.observablehq.com/u/abitrolly)\
**Post date:** [July 30, 2020, 7:17pm UTC](https://talk.observablehq.com/t/typeerror-convert-is-not-a-constructor/3657/4 "2020-07-30T19:17:55Z")

</div>

My first notebook 😊

> **[ANSI Text Viewer](https://observablehq.com/@abitrolly/ansi-text-viewer)**
>
> Using https://www.npmjs.com/package/ansi-to-html

---

<div class="post-metadata">

**Author:** ![aaronkyle](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/aaronkyle/32/8106_2.png) [@aaronkyle](https://talk.observablehq.com/u/aaronkyle)\
**Post date:** [July 30, 2020, 7:57pm UTC](https://talk.observablehq.com/t/typeerror-convert-is-not-a-constructor/3657/5 "2020-07-30T19:57:45Z")

</div>

Nice work solving your own question!

---

<div class="post-metadata">

**Author:** ![waynesutton](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/waynesutton/32/5040_2.png) [@waynesutton](https://talk.observablehq.com/u/waynesutton)\
**Post date:** [August 2, 2020, 3:40am UTC](https://talk.observablehq.com/t/typeerror-convert-is-not-a-constructor/3657/6 "2020-08-02T03:40:55Z")

</div>

Nice [abitrolly](https://talk.observablehq.com/u/abitrolly) Glad you were able to figure it out and congrats on your first notebook!
