Saffron is a pure ESM package. If your application is also written as and compiled to pure ESM, you can import Saffron using ESM import syntax:
cli.ts
import*asclifrom'@darkobits/saffron';
Or, if you prefer named imports:
cli.ts
import{command,init}from'@darkobits/saffron';
CommonJS
If your application is written in CommonJS, don't worry! You can still use Saffron with a dynamic import. However, dynamic imports must be await-ed, and CommonJS does not support top-level await statements, so the logic in your CLI must be wrapped in a function where we can then properly import Saffron:
cli.ts
asyncfunctionmain(){constcli=awaitimport('@darkobits/saffron'); // Or, if you prefer named imports:const{command,init}=awaitimport('@darkobits/saffron'); // Define your CLI here rather than at the top-level.}// Then, immediately invoke the above function. Easy peasy!voidmain();
If you are writing source using ESM syntax and then transpiling to CommonJS, you must make sure that your transpiler preserves dynamic import() statements (rather than transpiling them to require() calls). If dynamic imports are converted to require calls, your end-users will get an ERR_REQUIRE_ESM error when they try to use your application's CLI.
Examples in this guide will use ESM import syntax for its simplicity and brevity.