
=======
Modules
=======

Nonstraightforward supports JavaScript modules. Currently, only ES modules are
supported, however CommonJS support will come sometime in the future.

############################
Importing JavaScript modules
############################

Importing a JavaScript module can be done with

.. code-block:: text

    from module "./my_module.js" request defaultExport;
    from module "./my_module2.js" request { factorial, fibonacci, system_shredder };
    from module "node:fs/promises" request everything as fs;

The first form is a default import, which in JavaScript is

.. code-block:: text

    import defaultExport from "./my_module.js";

The second form is an import list (or whatever it's called) which compiles to

    import { factorial, fibonacci, system_shredder } from "./my_module2.js";

The third form (other than importing a Node.js module) is a library import which
always requires an alias to be provided.

.. code-block:: text

    import * as fs from "node:fs/promises";

####################################
Importing Nonstraightforward modules
####################################

Importing Nonstraightforward modules is as simple as replacing ``.js`` with
``.nstrfwd``, as shown below.

.. code-block:: text
    :caption: The top of ``src/main.nstrfwd`` `as of C9 2026.05.16`_

    from module "./tokenizer.nstrfwd" request tokenize;
    from module "./parser.nstrfwd" request { parse };
    from module "./codegen.nstrfwd" request { emit_script };
    from module "./errors.nstrfwd" request { nonstraightforward_shut_up, nonstraightforward_dont_care, nonstraightforward_error_count };
    from module "node:fs/promises" request everything as fs;
    from module "node:util" request everything as util;

In the example above, ``tokenizer.nstrfwd``, ``parser.nstrfwd``,
``codegen.nstrfwd``, and ``errors.nstrfwd`` are Nonstraightforward modules. They
used to require the ``.nstrfwd.js`` extension (the file extension used by the
compiler for output files) but as of C9 2026.05.16, the code generator replaces
``.nstrfwd.js`` automatically.

.. _as of C9 2026.05.16: https://codeberg.org/raiseafloppafan3925/nonstraightforward/commit/b7e5482eaef43e5e08298c2e102a79e735faa2dd#diff-67bc6152f5022b4eaa6e948a3e3d0ae1aac5a301
