Skip to main content

tio.js npm downloads prettier

A small TypeScript library that lets you evaluate code in a sandboxed environment everywhere.

Installation

Node.js

In your shell:

$ npm install tio.js

In your code:

import tio from 'tio.js'
Deno

In your code:

import tio from 'npm:tio.js'
Bun

In your shell:

$ bun install tio.js

In your code:

import tio from 'tio.js'

Examples

Getting a list of available languages
console.log(tio.languages)
Evaluating a string

Evaluating a string is really simple.

// Evaluate a code (Node.js is the default language).
let response = await tio('console.log("Hello, World!");')

console.log(response)

// Evaluate a code from another programming language (e.g. Python).
response = await tio('print("Hello, World!")', 'python3')

console.log(response)

Console output (for the first console.log):

{
  output: 'Hello, World!\n',
  language: 'javascript-node',
  timedOut: false,
  realTime: 0.069,
  userTime: 0.069,
  sysTime: 0.069,
  CPUshare: 99.99,
  exitCode: 0
}
Setting a default language

Set a default language so you don’t have to repeat the same arguments all over again.

tio.defaultLanguage = 'python3'

const response = await tio('print("Hello, World!")')

console.log(response)
Timeouts

Use this to contain scripts that runs longer than it should’ve been. (e.g. infinite loop)

// Make the response time out after waiting for 10000 ms (10 seconds).
const response = await tio('for (;;);', 'javascript-node', 10000)

console.log(response)

Console output:

{
  output: 'Request timed out after 10000ms',
  language: 'javascript-node',
  timedOut: true,
  realTime: 10,
  userTime: 10,
  sysTime: 10,
  CPUshare: 0,
  exitCode: 0
}
Setting a default timeout

Just like setting a default language beforehand, you can set default timeouts so you don’t have to enter the same arguments again.

tio.defaultTimeout = 10000

const response = await tio('for (;;);', 'javascript-node')

console.log(response) // Does the same as the example before.