Skip to content

Repository files navigation

motokonpm versionGitHub licensePRs Welcome

Compile and run Motoko smart contracts in Node.js or the browser.


Installation:

npm i --save motoko

Examples:

Basic usage

importmofrom'motoko';// -- OR --constmo=require('motoko');// Create a Motoko script in a virtual file systemmo.write('Main.mo',` actor { public query func hello() : async Text { "Hello, JavaScript!" }; }`);// Generate the corresponding Candid interfaceconsole.log(mo.candid('Main.mo'));

Evaluate a program

mo.write('Main.mo',` actor Main { public query func hello() : async Text { "Hello, world!" }; }; await Main.hello();`)mo.run('Main.mo');

Evaluate a program (shorthand)

mo.file('Main.mo').write('actor Main { public query func getNumber() : async Nat { 5 } }').run();

Load dependencies from GitHub

mo.clearPackages();awaitmo.installPackages({core: 'caffeinelabs/motoko-core/main/src',// import "mo:core/...";});

Generate parse trees

// Generate a Motoko ASTconsole.log(mo.parseMotoko('actor Main { public query func test() : async Nat { 123 } }'));// Generate a Candid ASTconsole.log(mo.parseCandid('service : { test : () -> (nat) }'));

Optimize for browsers

// Load just the `write()`, `loadPackages()`, `clearPackages()`, and `run()`, operations for a smaller file size:importmofrom'motoko/interpreter';

API:

Top-level API

// Read the contents of a virtual filemo.read(path)// Write a string to a virtual filemo.write(path,string)// Rename a virtual filemo.rename(path,newPath)// Delete a virtual filemo.delete(path)// List the files in a virtual directorymo.list(path)// Fetch a package from GitHub or jsDelivrawaitmo.fetchPackage(name,source);awaitmo.fetchPackage('base','caffeinelabs/motoko-base/master/src');// Try to fetch and load packages from GitHub or jsDelivrawaitmo.installPackages({[packageName]: repositoryPath, ... })// Load a value returned from `fetchPackage()`mo.loadPackage(package)// Use a virtual directory as a packagemo.usePackage(packageName,directory)// Clear loaded packagesmo.clearPackages()// Ensure that a package is correctly formattedmo.validatePackage(package)// Configure the compiler to resolve `import "mo:{alias}";` -> `import "canister:{id}";`// `directory` should contain `*.did` files for canister dependenciesmo.setAliases(directory,{alias: id, ... })// Set the public metadata (an array of strings) used by the compilermo.setPublicMetadata(strings)// Set the maximum number of interpreter steps before cancelling a `run()` invocationmo.setRunStepLimit(limit)// Generate errors and warnings for a Motoko programmo.check(path)// Run a Motoko program with optional virtual library pathsmo.run(path)mo.run(path,[libraryPath, ...])// Generate the Candid interface for a Motoko programmo.candid(path)// Compile a Motoko program to WebAssemblymo.wasm(path,'ic')// IC interface format (default)mo.wasm(path,'wasi')// WASI interface format// Return the parse tree for a Candid stringmo.parseCandid(candidString)// Return the parse tree for a Motoko stringmo.parseMotoko(motokoString)// Return the typed parse tree for a Motoko file path (or array of paths)mo.parseMotokoTyped(path)mo.parseMotokoTyped(paths=[...])// Find the 'Main.mo' file or an equivalent canister entry pointmo.resolveMain(directory='')// Find the 'Lib.mo' file or an equivalent library entry pointmo.resolveLib(directory='')// Get the compiler version ('latest' by default)mo.version// Access the underlying Motoko compilermo.compiler

File API

// Create an object representing a virtual fileconstfile=mo.file('Main.mo')// Get the file pathfile.path// Get another file object with the same pathfile.clone()// Read the file as a stringfile.read()// Write a string to the filefile.write(string)// Rename the filefile.rename(newPath)// Delete the filefile.delete()// List children (if a directory)file.list()// Generate errors and warnings for a Motoko programfile.check()// Run the file as a Motoko programfile.run()// Generate the Candid interface for a Motoko programfile.candid()// Compile the file to WebAssembly (see `mo.wasm()`)file.wasm('ic')file.wasm('wasi')// note: cannot contain actors// Parse the file as a Motoko programfile.parseMotoko()// Parse the file as a Candid interfacefile.parseCandid()// Parse the file (with types) as a Motoko programfile.parseMotokoTyped()

About

Compile and run Motoko smart contracts in Node.js or the browser.

Resources

Stars

19 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages