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”You can import node packages installed in your project
in .mjs or .mts.
import { parse } from "ini"
// static importconst res = parse("x = 1\ny = 2")console.log(res)
// dynamic import with top-level awaitconst { stringify } = await import("ini")console.log(stringify(res))JavaScript imports
Section titled “JavaScript imports”You can also import other local JavaScript module files (using static or dynamic imports).
Use .mjs extension for module JavaScript files.
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)TypeScript imports
Section titled “TypeScript imports”You can import TypeScript module files (.mts).
Use .mts extension for module TypeScript files.
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)env.generator
Section titled “env.generator”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.`}JSON Modules
Section titled “JSON Modules”You can import JSON files using the import statement and get automatic type inference.
{ "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.
import data from "./data.json" with { type: "json" }
console.log(data.name) // GenAIScriptDefault function export
Section titled “Default function export”If you set a function as the default export, GenAIScript will call it. The function can be async.
script(...)export default async function() { $`Write a poem.`}Package type
Section titled “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.
{ "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”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.
// convert file:// to absolute pathconst filename = path.resolveFileURL(import.meta.url)