2023-12-27 12:17:43 +00:00
---
id: import-external-libraries-using-runjs
title: Import external libraries using RunJS
---
2024-02-23 07:01:23 +00:00
ToolJet allows you to integrate external JavaScript libraries into your application using RunJS queries. This guide walks you through the process of importing and utilizing these libraries effectively.
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
## Choosing Libraries
You can import various JavaScript libraries using their Content Delivery Network (CDN) links. Find the CDN links for your desired open-source projects on [jsDelivr ](https://www.jsdelivr.com/ ).
## Creating a new app and runJS query
Start by creating a new application in ToolJet. Then, proceed to create a new RunJS query from the query panel.
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
< div style = {{textAlign: ' center ' } } >
2024-04-11 06:04:27 +00:00
< img style = {{ border: ' 0 ' , marginBottom: ' 15px ' , borderRadius: ' 5px ' , boxShadow: ' 0px 1px 3px rgba ( 0 , 0 , 0 , 0 . 2 ) ' } } className = "screenshot-full" src = "/img/how-to/import-js/create-new-query.png" alt = "Create a new RunJS query" / >
2024-02-23 07:01:23 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
## Importing Libraries
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
Here's a step-by-step guide to importing libraries and displaying an alert upon successful import.
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
```js
// Function to add script dynamically
function addScript(src) {
2023-12-27 12:17:43 +00:00
return new Promise((resolve, reject) => {
2024-02-23 07:01:23 +00:00
const scriptTag = document.createElement('script');
scriptTag.setAttribute('src', src);
scriptTag.addEventListener('load', resolve);
scriptTag.addEventListener('error', reject);
document.body.appendChild(scriptTag);
2023-12-27 12:17:43 +00:00
});
2024-02-23 07:01:23 +00:00
}
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
try {
// Importing MathJS
2023-12-27 12:17:43 +00:00
await addScript('https://cdn.jsdelivr.net/npm/mathjs@11.7.0');
2024-02-23 07:01:23 +00:00
// Importing FlattenJS
2023-12-27 12:17:43 +00:00
await addScript('https://cdn.jsdelivr.net/npm/flattenjs@2.1.3/lib/flatten.min.js');
2024-02-23 07:01:23 +00:00
// Showing a success alert
await actions.showAlert("success", 'Mathjs and Flatten imported');
} catch (error) {
console.error(error);
}
```
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
After creating and running the query, an alert should pop up with the message "Mathjs and Flatten imported."
2023-12-27 12:17:43 +00:00
:::tip
Enable the **Run this query on application load?** option to make the libraries available throughout the application as soon as the app is loaded.
:::
2024-02-23 07:01:23 +00:00
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
< div style = {{textAlign: ' center ' } } >
2024-04-11 06:04:27 +00:00
< img style = {{ border: ' 0 ' , marginBottom: ' 15px ' , borderRadius: ' 5px ' , boxShadow: ' 0px 1px 3px rgba ( 0 , 0 , 0 , 0 . 2 ) ' } } className = "screenshot-full" src = "/img/how-to/import-js/import-successful.png" alt = "Import Successful" / >
2024-02-23 07:01:23 +00:00
< / div >
< / div >
2023-12-27 12:17:43 +00:00
## Examples
2024-02-23 07:01:23 +00:00
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
### 1. Flattening JSON Objects using FlattenJS
Create a new RunJS query using the Flatten library (imported earlier) to flatten a JSON object.
```js
return flatten({
key1: {
keyA: 'valueI'
},
key2: {
keyB: 'valueII'
},
key3: { a: { b: { c: 2 } } }
});
```
Preview the output in the query manager or run the query to see the flattened JSON.
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
< div style = {{textAlign: ' center ' } } >
2024-04-11 06:04:27 +00:00
< img style = {{ border: ' 0 ' , marginBottom: ' 15px ' , borderRadius: ' 5px ' , boxShadow: ' 0px 1px 3px rgba ( 0 , 0 , 0 , 0 . 2 ) ' } } className = "screenshot-full" src = "/img/how-to/import-js/flatten-js.png" alt = "Use FlattenJS" / >
2024-02-23 07:01:23 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
< div style = {{paddingTop:'24px', paddingBottom: ' 24px ' } } >
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
### 2. Computation using MathJS
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
Create another RunJS query utilizing the MathJS library for a calculation.
2023-12-27 12:17:43 +00:00
```js
2024-02-23 07:01:23 +00:00
return math.atan2(3, -3) / math.pi;
2023-12-27 12:17:43 +00:00
```
2024-02-23 07:01:23 +00:00
Preview the output, or Run the query to see the result of the calculation.
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
< div style = {{textAlign: ' center ' } } >
2024-04-11 06:04:27 +00:00
< img style = {{ border: ' 0 ' , marginBottom: ' 15px ' , borderRadius: ' 5px ' , boxShadow: ' 0px 1px 3px rgba ( 0 , 0 , 0 , 0 . 2 ) ' } } className = "screenshot-full" src = "/img/how-to/import-js/math-js-v2.png" alt = "Use MathJs" / >
2024-02-23 07:01:23 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
< / div >
2023-12-27 12:17:43 +00:00
2024-02-23 07:01:23 +00:00
This guide provides a clear and detailed walkthrough for importing external JavaScript libraries into your ToolJet application.