docs/versioned_docs/version-2.62.0/how-to/import-external-lib-js.md
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.
</div> <div style={{paddingTop:'24px', paddingBottom:'24px'}}>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.
</div> <div style={{paddingTop:'24px', paddingBottom:'24px'}}>Once the query is created, add the following code:
<div style={{paddingTop:'24px', paddingBottom:'24px'}}>// Function to add script dynamically
function addScript(src) {
return new Promise((resolve, reject) => {
const scriptTag = document.createElement('script');
scriptTag.setAttribute('src', src);
scriptTag.addEventListener('load', resolve);
scriptTag.addEventListener('error', reject);
document.body.appendChild(scriptTag);
});
}
try {
// Importing MathJS
await addScript('https://cdn.jsdelivr.net/npm/[email protected]');
// Importing FlattenJS
await addScript('https://cdn.jsdelivr.net/npm/[email protected]/lib/flatten.min.js');
// Showing a success alert
await actions.showAlert("success", 'Mathjs and Flatten imported');
} catch (error) {
console.error(error);
}
After adding the code, click on the Run button in the query panel, an alert should pop up with the message "Mathjs and Flatten imported."
:::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. :::
<div style={{textAlign: 'center'}}> </div> </div> <div style={{paddingTop:'24px', paddingBottom:'24px'}}>return flatten({
key1: {
keyA: 'valueI'
},
key2: {
keyB: 'valueII'
},
key3: { a: { b: { c: 2 } } }
});
return math.atan2(3, -3) / math.pi;
This guide provides a clear and detailed walkthrough for importing external JavaScript libraries into your ToolJet application.