# Any alternative to eval() ?

**URL:** https://talk.observablehq.com/t/any-alternative-to-eval/4689
**Category:** Help
**Created:** [February 22, 2021, 1:45pm UTC](https://talk.observablehq.com/t/any-alternative-to-eval/4689 "2021-02-22T13:45:30Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Real-John-Cheung](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/real-john-cheung/32/4489_2.png) [@Real-John-Cheung](https://talk.observablehq.com/u/Real-John-Cheung)
#### Post date: [February 22, 2021, 1:45pm UTC](https://talk.observablehq.com/t/any-alternative-to-eval/4689/1 "2021-02-22T13:45:30Z")

</div>

Currently I am working on a note book.

it require a module online  
`ObjectA = require(url)`  
it also has a attached js file with content like this  
`ObjectA.functionB()`

When I try to run the js file in the notebook I can only use eval() like this  
`jsString = FileAttachment("theJS.js").text()` with  
`eval(jsString)`

I have try  
`import (await FileAttachment("theJS.js").url())` and  
`new Function(jsString)()` and similar method and they all return error of `ObjectA is not defined`

Wonder if there is any way to do that without using eval()

---

<div class="post-metadata">

### Author: ![tomlarkworthy](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tomlarkworthy/32/5940_2.png) [@tomlarkworthy](https://talk.observablehq.com/u/tomlarkworthy)
#### Post date: [February 22, 2021, 2:13pm UTC](https://talk.observablehq.com/t/any-alternative-to-eval/4689/2 "2021-02-22T14:13:59Z")

</div>

Mike Bostock does this:-

```auto
morph = require(await FileAttachment("nanomorph-5.4.2.js").url())

```

in

> **[Hello, nanomorph!](https://observablehq.com/@mbostock/hello-nanomorph#morph)**
>
> This notebook demonstrates how to use nanomorph and Hypertext Literal to construct an Observable view of a TODO list.

Its all dependant on how the JS is packed in the file though. If the above does not work send us a link as we don’t have enough context to know.

---

<div class="post-metadata">

### Author: ![Real-John-Cheung](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/real-john-cheung/32/4489_2.png) [@Real-John-Cheung](https://talk.observablehq.com/u/Real-John-Cheung)
#### Post date: [February 22, 2021, 3:09pm UTC](https://talk.observablehq.com/t/any-alternative-to-eval/4689/3 "2021-02-22T15:09:42Z")

</div>

thx for the reply!  
Sorry for not being clear enough for my question  
here is a simple page that (I hope) describe the problem I have

> **[Min-Example](https://observablehq.com/@real-john-cheung/min-example)**
>
> I want to use hljs on my notebooks, and I have a customized hljs mode file to load... so I first import hljs customized hljs mode files are packed js files with content like this: hljs.registerLanguage("nameOfLang",(()=\>{"use strict";return...

Cheers!

---

<div class="post-metadata">

### Author: ![tomlarkworthy](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tomlarkworthy/32/5940_2.png) [@tomlarkworthy](https://talk.observablehq.com/u/tomlarkworthy)
#### Post date: [February 22, 2021, 6:12pm UTC](https://talk.observablehq.com/t/any-alternative-to-eval/4689/4 "2021-02-22T18:12:36Z")

</div>

Given this is a single line of JS I put it in a notebook cell.

> **[Min-Example](https://observablehq.com/d/f409f681d87aef40)**
>
> I want to use hljs on my notebooks, and I have a customized hljs mode file to load... so I first import hljs customized hljs mode files are packed js files with content like this: hljs.registerLanguage("nameOfLang",(()=\>{"use strict";return...

I give the cell a name and reference it before where I need it installed so the reactive runtime executes the riscript installer is called before trying to highlight anything.

Of course, you can import that riscript installer cell from another notebook that uses highlighter. So I think this is a good way of formulating it.

---

<div class="post-metadata">

### Author: ![bgchen](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bgchen/32/784_2.png) [@bgchen](https://talk.observablehq.com/u/bgchen)
#### Post date: [February 22, 2021, 6:17pm UTC](https://talk.observablehq.com/t/any-alternative-to-eval/4689/5 "2021-02-22T18:17:31Z")

</div>

For `highlight.js` specifically, you might take a look at the approach taken in this notebook:

> **[highlightjs for Lean](https://observablehq.com/@bryangingechen/highlightjs-for-lean)**
>
> Taken from https://github.com/highlightjs/highlight.js/pull/1689 by Patrick Massot. Now published on GitHub at https://github.com/leanprover-community/highlightjs-lean. Import with: import {hljs} from "@bryangingechen/highlightjs-for-lean" Markdown...

Basically it uses a trick (due to @mootari) to add the highlighter to Observable’s `hljs`.

---

<div class="post-metadata">

### Author: ![Real-John-Cheung](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/real-john-cheung/32/4489_2.png) [@Real-John-Cheung](https://talk.observablehq.com/u/Real-John-Cheung)
#### Post date: [February 23, 2021, 7:02am UTC](https://talk.observablehq.com/t/any-alternative-to-eval/4689/6 "2021-02-23T07:02:08Z")

</div>

hmm…  
The reason why I want to load the attached js file instead of copy and paste the content of it is that the customised hljs model might be updated in the future.  
The goal is that when the model is updated I can simply replace the attached file without any more changes of the notebook…

---

<div class="post-metadata">

### Author: ![tomlarkworthy](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tomlarkworthy/32/5940_2.png) [@tomlarkworthy](https://talk.observablehq.com/u/tomlarkworthy)
#### Post date: [February 23, 2021, 8:10am UTC](https://talk.observablehq.com/t/any-alternative-to-eval/4689/7 "2021-02-23T08:10:21Z")

</div>

Yeah that’s why I brought up that it can be imported as normal. Put that snippet in its own dedicated notebook and you can maintain that independently to your core notebook.

Copy and pasting that snippet is less work than anything else, and you decouple the maintenance. In a sense, you use a notebook to bundle that snippet, as a notebook IS a module. By pasting the code in a cell, you are bundling the code into a module for ease of consumption in other notebooks.

---

<div class="post-metadata">

### Author: ![Real-John-Cheung](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/real-john-cheung/32/4489_2.png) [@Real-John-Cheung](https://talk.observablehq.com/u/Real-John-Cheung)
#### Post date: [February 23, 2021, 12:38pm UTC](https://talk.observablehq.com/t/any-alternative-to-eval/4689/8 "2021-02-23T12:38:03Z")

</div>

I see you point…  
I think I will go for that for now  
Thx a lot!

though still hope that there can be a way of just loading the file
