ECMAScript Modules
Kosko supports ECMAScript modules (ESM). You can write components and environments in native ESM files.
Node.js v12 or above is required for ESM. However, Node.js v14.8.0 or above is recommended because top-level await is supported without flags in this version.
#
EnablingYou don't have to modify any config in kosko.toml
at all. To start using ESM, you can either set type
in package.json
as below.
Or rename file extension as .mjs
. See Node.js documentation for more information.
#
EnvironmentsWhen @kosko/env
is imported in ESM, it returns a Promise
instead. This is because import()
is asynchronous. You MUST add await
when retrieving environment variables as below.
#
Programmatical UsageYou can use @kosko/env
and @kosko/generate
package directly. The following is a basic example.
There's no much difference between CommonJS and ESM when using Kosko programmatically. However, it could be troublesome if you are going to use both CommonJS and ESM at the same time. In Node.js, ESM has its own separate cache, which means there are two isolated instances of Environment
in CommonJS and ESM and both must be initialized separately.
It's recommended to use only one module type. If you really need to use both, here's an example of how to initialize them.