Skip to main content
Deno 2 is finally here šŸŽ‰ļø
Learn more

tio.js CI npm downloads prettier

Evaluate code in a sandboxed environment everywhere with Node.js or Deno.

Node.js

In the shell, do:

npm i tio.js

In your source:

import tio from 'tio.js';

Deno

In your source:

import tio from 'https://deno.land/x/tio@v3.0.2/mod.ts';

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.