Org-babel support for evaluating typescript code, based on support of ts-node. In contrast to ob-typescript, ob-ts-node can support CommonJS-style imports without too much of a hassle:
#+BEGIN_SRC typescript const https = require('https'); https.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', (resp) => { let data = ''; // A chunk of data has been recieved. resp.on('data', (chunk) => { data += chunk; }); resp.on('end', () => { console.log(JSON.parse(data).title); }); }).on("error", (err) => { console.log("Error: " + err.message); }); #+END_SRC #+RESULTS: : Geminid Meteors over Xinglong Observatory
The project is more or less in beta. But still, one can benefit from using it.
- [X] Basic support for evaluation
- [ ] Better support for customization
- [ ] Support for `:session`
Using use-package and quelpa packages (recommended):
(use-package ob-ts-node
:after org typescript
:quelpa
(ob-ts-node :repo "tmythicator/ob-ts-node"
:fetcher github))
(add-to-list 'load-path "/path/to/ob-ts-node.el")
(require 'ob-ts-node)
npm i -g node typescript ts-node
(org-babel-do-load-languages
'org-babel-load-languages
'((ts-node . t)
))
- :cli-args:
Use this header to prepend
ts-node
CLI arguments for evaluation. This would be equal to executingts-node <my-args> <my-org-babel-snippet>
:#+BEGIN_SRC typescript :cli-args "--project /path/to/tsconfig.json" // your code goes here #+END_SRC
- :cli-override:
Use this header to completely override the arguments of
ts-node
. This would be equal to executingts-node <my-args>
:#+BEGIN_SRC typescript :cli-override "-v" console.log(43) #+END_SRC #+RESULTS: : v9.1.1
TSError: ⨯ Unable to compile TypeScript:
../../../../../tmp/babel-abcde/ts-fghij.ts(1,15): error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
If this error appears, running this command locally to org-file should solve the problem:
npm i --save-dev @types/node