Skip to content

tmythicator/ob-ts-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Org Babel support for Typescript via ts-node

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

Status [1/3]

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`

Installation

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))

Manual installation:

(add-to-list 'load-path "/path/to/ob-ts-node.el")
(require 'ob-ts-node)

Node.js requirements installation:

npm i -g node typescript ts-node

Configuration

Append ts-node to the org-babel-load-languages

(org-babel-do-load-languages
 'org-babel-load-languages
 '((ts-node . t)
   ))

Supported header args

  • :cli-args: Use this header to prepend ts-node CLI arguments for evaluation. This would be equal to executing ts-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 executing ts-node <my-args>:
    #+BEGIN_SRC typescript :cli-override "-v"
      console.log(43)
    #+END_SRC
    
    #+RESULTS:
    : v9.1.1
        

Troubleshooting

Common TSError: missing @types/node dependency

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

About

Literate programming in Typescript made easy

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published