Unified Module Definitions (UMD), which is compatible with both AMD and CommonJS. UMD modules are also able to be executed without being imported or when no module system is present. This is the default option for the
Asynchronous Module Definitions (AMD), which is in particular used by the RequireJS library.
CommonJS, widely used by Node.js/npm (
Plain. Don't compile for any module system. You can access a module by its name in the global scope.
If you're targeting the browser and want to use a different module system than UMD, you can specify the desired module type in the
webpackTask configuration block. For example, to switch to CommonJS, use:
Webpack provides two different "flavors" of CommonJS,
commonjs2, which affect the way your declarations are made available. While in most cases, you probably want
commonjs2, which adds the
module.exports syntax to the generated library, you can also opt for the "pure"
commonjs option, which implements the CommonJS specification exactly. To learn more about the difference between
commonjs2, check here.
Choose the target module system
To select module kind, set the
moduleKind compiler option in the Gradle build script.
Available values are:
In the Kotlin Gradle DSL, there is also a shortcut for setting the CommonJS module kind:
To tell Kotlin that an
@JsModule annotation. Consider you have the following CommonJS module called "hello":
You should declare it like this in Kotlin:
Apply @JsModule to packages
Files marked with
@file:JsModule annotation can't declare non-external members. The example below produces a compile-time error:
Import deeper package hierarchies
.kt file for each package you import.
For example, let's make the example a bit more complicated:
To import this module in Kotlin, you have to write two Kotlin source files:
When a declaration is marked as
.js files that you can copy to your project's static resources and include via a
<script> tag. To tell Kotlin that it's okay to use a
@JsModule declaration from a non-module environment, add the
You could describe it from Kotlin as follows:
Module system used by the Kotlin Standard Library
Kotlin is distributed with the Kotlin/JS standard library as a single file, which is itself compiled as an UMD module, so you can use it with any module system described above. While for most use cases of Kotlin/JS, it is recommended to use a Gradle dependency on
kotlin-stdlib-js, it is also available on NPM as the