Skip to content

Modules

import express from ///express///
import [Hono] from ///hono///
import [describe, it, expect] from ///vitest///
import axios, [AxiosError] from ///axios///
import all as fs from ///fs///
import express from "express";
import { Hono } from "hono";
import { describe, it, expect } from "vitest";
import axios, { AxiosError } from "axios";
import * as fs from "fs";

You can also write imports with the module path first:

from ///express/// import express
from ///hono/// import [Hono]
from ///vitest/// import [describe, it, expect]
from ///axios/// import axios, [AxiosError]
from ///fs/// import all as fs

This compiles to the same JavaScript as the import...from syntax above.

Import a module for its side effects only (no bindings):

import ///dotenv/config///
import ///./polyfills///
import "dotenv/config";
import "./polyfills";

Use the with keyword to specify import attributes:

import package from ///./package.json/// with [ type be ///json/// ]
import [name; version] from ///./package.json/// with [ type be ///json/// ]
import package from "./package.json" with { type: "json" };
import { name, version } from "./package.json" with { type: "json" };

The from...import syntax also supports with:

from ///./data.json/// import data with [ type be ///json/// ]
from ///./package.json/// import [name, version] with [ type be ///json/// ]
import data from "./data.json" with { type: "json" };
import { name, version } from "./package.json" with { type: "json" };
use std.math
from std.math use sin, cos
import * as math from "std/math";
import { sin, cos } from "std/math";
public fn greet name to console.log[name]
public const VERSION be ///1.0///
export default fn main
console.log[///hi///]
export function greet(name) { console.log(name); }
export const VERSION = "1.0";
export default function main() {
console.log("hi");
}
namespace utils
fn helper
return 42
const utils = (() => {
function helper() {
return 42;
}
})();

By default, .purus files are compiled as ES Modules (ESM). You can configure the module type to CommonJS using the --type CLI option, config.purus, or package.json.

  1. CLI --type option (highest priority)
  2. config.purus type field
  3. package.json type field
  4. Default: module (ESM)
Terminal window
purus build --type commonjs
purus build --type module
const type be ///module///

or

const type be ///commonjs///

Values are the same as package.json’s type field: module (ESM) or commonjs (CJS).

When module type is set to commonjs, imports and exports are compiled to CJS syntax:

import express from ///express///
import [Hono] from ///hono///
import all as fs from ///fs///
import ///dotenv/config///
const express = require("express");
const { Hono } = require("hono");
const fs = require("fs");
require("dotenv/config");
public const VERSION be ///1.0///
export default 42
const VERSION = "1.0";
exports.VERSION = VERSION;
module.exports = 42;
const fs be require[///fs///]
const fs = require("fs");