Documentation Index
Fetch the complete documentation index at: https://mintlify.com/quickjs-ng/quickjs/llms.txt
Use this file to discover all available pages before exploring further.
Creating and Using Modules
QuickJS supports ES6 modules withimport and export statements. This tutorial shows how to create and use both JavaScript and native C modules.
JavaScript Modules
Create a module
Create The
fib_module.js with an exported function:export keyword makes the fib function available to other modules.Import and use the module
Create The
hello_module.js that imports the function:import statement loads the module and destructures the exported function.Native C Modules
You can also import native modules compiled as shared libraries.Create a cross-platform import
Create Key features:
test_fib.js that imports a native module:- Uses
qjs:osto detect the platform - Dynamically imports
.soon Unix or.dllon Windows - Uses
await import()for dynamic imports
Compile the native module
First, create the C module (see Native Functions guide).Then compile it as a shared library:On Linux/macOS:On Windows:
Built-in Modules
QuickJS provides built-in modules accessible with theqjs: prefix:
qjs:os- Operating system interfaces (file I/O, processes, etc.)qjs:std- Standard utilities (console, setTimeout, etc.)qjs:bjson- Binary JSON encoding/decoding
--std flag:
Module Resolution
QuickJS resolves module specifiers as follows:- Relative paths (
./,../) - Resolved relative to the importing file - Built-in modules (
qjs:*) - QuickJS standard library - Absolute paths (
/path/to/module) - Loaded directly - Bare specifiers (
module-name) - Not supported by default
Dynamic Imports
Useawait import() for runtime module loading:
Compiling Modules
Compile a module to bytecode:-m flag tells qjsc to compile as an ES6 module.
Next Steps
- Learn about native C functions
- Explore bytecode compilation
- Read about embedding QuickJS