> For the complete documentation index, see [llms.txt](https://docs.sacruda.nl/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sacruda.nl/developers/your-apps-code/import-and-export.md).

# Import and export

The environment that third-party applications run in is far from ordinary, mostly because we're dealing with the Sacruda filesystem instead of regular old web assets. Unfortunately this means that importing and exporting between JS files is also a little bit... unorthodox. You can't use a regular `require` call or `import` keyword.

### What's different from normal JS?

* You have access to top-level await and return statements
* There is no `import`, `export` or `require`&#x20;
* TPA javascript has a lot more global variables, classes and properties, which you've undoubtedly encountered already.

### Importing and exporting

Say you have a class in `tray.js` that you want to make use of in `main.js`. This is quite simple! First, at the bottom of `tray.js`, export the class by "returning" it:

<pre class="language-javascript" data-title="tray.js"><code class="lang-javascript">class MyClass {
  // some code
}

<strong>return { MyClass };
</strong></code></pre>

Then, inside `main.js`, destructure the result of an awaited call to the `load` function to get the class:

{% code title="main.js" %}

```javascript
const { MyClass } = await load("tray.js");
```

{% endcode %}

{% hint style="info" %}
The `load` function is asynchronous: it has to be awaited.
{% endhint %}

Assuming none of the files in the "import chain" had syntactic errors, your class is now available in `main.js`.

### Behind the scenes

Internally, when the text of your script file is loaded by `JsExec`, it is wrapped in a `export default async function` before being sent off to the backend, which returns a link that's loaded using a dynamic import. This extra step is put in place to give the wrapped version of your code a clear filename. Without this step, I'm forced to load the JS using Data URLs, which makes stack traces virtually unreadable. The `wrap` function takes an object that contains all variables, functions, classes and other globals made accessible to third-party apps. Observe this function that I just copied from `JsExec`:

{% code title="src/ts/jsexec/index.ts: class JsExec extends Process" %}

```typescript
  private wrap(contents: string) {
    if (!this.props) throw new JsExecError(`No TPA props to use`);

    return `export default async function({${Object.keys(this.props).join(",")}}) {\nconst global = arguments;\n${contents}\n}`;
  }
```

{% endcode %}

This wrapper takes your code and wraps it into a function. This is exactly the reason why top-level awaits work and why you have to return stuff instead of exporting them. The `props` property here is the Third-party app props object assembled before this function is first called. I'm automatically destructuring the props to make them easily accessible to you. You're welcome.

{% hint style="info" %}
JsExec is the magic sauce that makes it so that third-party JS files are run reliably and securely.<br>

{% code lineNumbers="true" %}

```typescript
/**
 * Sacruda JavaScript Execution Engine
 *
 * This file executes JS files in ArcOS under a relatively controlled environment.
 * It is part of the ArcOS TPA framework: a system for running third-party apps.
 *
 * All rights belong to their respective authors.
 *
 * © IzKuipers 2025
 */
```

{% endcode %}
{% endhint %}
