# Imports

> Learn how to enable module imports in GenAI scripts by converting them to .mjs format and using static or dynamic imports.

Scripts using the `.mjs` extension can use static or dynamic imports as any other module file. You can rename any `.genai.js` file to `.genai.mjs` to enable module imports.

## Module Imports

[Section titled “Module Imports”](#module-imports)

You can import node packages installed in your project in `.mjs` or `.mts`.

script.genai.mjs

```js
import { parse } from "ini"


// static import
const res = parse("x = 1\ny = 2")
console.log(res)


// dynamic import with top-level await
const { stringify } = await import("ini")
console.log(stringify(res))
```

## JavaScript imports

[Section titled “JavaScript imports”](#javascript-imports)

You can also import other local **JavaScript** module files (using static or dynamic imports). **Use `.mjs` extension for module JavaScript files.**

summarizer.mjs

```js
export function summarize(files) {
    def("FILE", files)
    $`Summarize each file. Be concise.`
}
```

* static import (`import ... from ...`)

```js
import { summarize } from "./summarizer.mjs"
summarize(env.generator, env.files)
```

* dynamic import (`async import(...)`)

```js
const { summarize } = await import("./summarizer.mjs")
summarize(env.generator, env.files)
```

## TypeScript imports

[Section titled “TypeScript imports”](#typescript-imports)

You can import [TypeScript module files](/genaiscript/reference/scripts/typescript) (`.mts`). **Use `.mts` extension for module TypeScript files.**

summarizer.mts

```js
export function summarize(files: string[]) {
    def("FILE", files)
    $`Summarize each file. Be concise.`
}
```

* static import (`import ... from ...`)

```js
import { summarize } from "./summarizer.mts"
summarize(env.generator, env.files)
```

* dynamic import (`async import(...)`)

```js
const { summarize } = await import("./summarizer.mts")
summarize(env.generator, env.files)
```

## `env.generator`

[Section titled “env.generator”](#envgenerator)

The `env.generator` references the root prompt generator context, the top level `$`, `def` functions… It can be used to create function that can be used with those function or also with `runPrompt`.

```js
export function summarize(_, files) {
    _.def("FILE", files)
    _.$`Summarize each file. Be concise.`
}
```

## JSON Modules

[Section titled “JSON Modules”](#json-modules)

You can import JSON files using the `import` statement and get automatic type inference.

data.json

```js
{
    "name": "GenAIScript"
}
```

Use the `with { type: "json" }` syntax to import JSON files in `.mjs` or `.mts` files. The file path is relative to the genaiscript source file.

script.genai.mts

```js
import data from "./data.json" with { type: "json" }


console.log(data.name) // GenAIScript
```

## Default function export

[Section titled “Default function export”](#default-function-export)

If you set a function as the default export, GenAIScript will call it. The function can be async.

poem.genai.mjs

```js
script(...)
export default async function() {
    $`Write a poem.`
}
```

## Package type

[Section titled “Package type”](#package-type)

If you have a `package.json` file in your project, you can set the `type` field to `module` to enable module imports in all `.js` files.

```json
{
    "type": "module"
}
```

This will allow you to use module imports in all `.js` files in your project.

## Current script file

[Section titled “Current script file”](#current-script-file)

You can use the `import.meta.url` to get the current script file URL. This is useful to get the current script file path and use it in your script.

script.genai.mjs

```js
// convert file:// to absolute path
const filename = path.resolveFileURL(import.meta.url)
```