Skip to content
A flat, minimalistic graphic shows five interconnected rectangles, each resembling documents with visible file extension icons like .mjs, .mts, and .json. The rectangles are linked by puzzle piece shapes, symbolizing module imports. A simple folder icon is present among them. The illustration uses five solid corporate colors, has no text or gradients, and appears in a pixelated 8-bit style with a transparent background and no people or decorative effects. The image size is small and square.

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.

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

script.genai.mjs
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))

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

summarizer.mjs
export function summarize(files) {
def("FILE", files)
$`Summarize each file. Be concise.`
}
  • static import (import ... from ...)
import { summarize } from "./summarizer.mjs"
summarize(env.generator, env.files)
  • dynamic import (async import(...))
const { summarize } = await import("./summarizer.mjs")
summarize(env.generator, env.files)

You can import TypeScript module files (.mts). Use .mts extension for module TypeScript files.

summarizer.mts
export function summarize(files: string[]) {
def("FILE", files)
$`Summarize each file. Be concise.`
}
  • static import (import ... from ...)
import { summarize } from "./summarizer.mts"
summarize(env.generator, env.files)
  • dynamic import (async import(...))
const { summarize } = await import("./summarizer.mts")
summarize(env.generator, env.files)

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.

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

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

data.json
{
"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
import data from "./data.json" with { type: "json" }
console.log(data.name) // GenAIScript

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

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

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.

{
"type": "module"
}

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

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
// convert file:// to absolute path
const filename = path.resolveFileURL(import.meta.url)