首页 > 解决方案 > ReferenceError: 将 kotlin 编译到 JS 浏览器时未定义模块

问题描述

我正在构建一个多平台的 Kotlin 项目。后端必须同时编译为JS BrowserJVM。我有 2 个平台的 2 个前端。问题是当我尝试运行代码时。gradle我可以毫无问题地构建它,但是在浏览器中我得到了一个ReferenceError: module is not defined在线venus.js:23643:1. 我不确定问题是什么。我已经为项目(korioklogger)添加了一些新的依赖项,但我确保我只使用两个平台上都可用的东西(我只使用来自 的WebSocketsBase64korio,据我所知,这里WebSockets提到的所有平台都支持) . 我现在被这个问题困扰了好几天。非常感谢任何帮助!谢谢!

这是我的build.gradle

group 'venus'
version '0.1.0'

buildscript {
    ext.kotlin_version = '1.3.70'
    ext.dokka_version = '0.10.1'
    ext.ktlint_version = '0.27.0'
    ext.korioVersion = "1.11.10"
    ext.kloggerVersion = "1.10.1"

    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
    }
}

apply plugin: 'kotlin2js'
apply plugin: 'kotlin-dce-js'
apply plugin: 'org.jetbrains.dokka'
//apply plugin: 'kotlin-platform-js'

runDceKotlinJs.keep 'venus.Driver.run', 'venusbackend.assembler.Linter', 'venus.api.API', "kotlin.Number"

runDceKotlinJs.dceOptions.devMode = true
runDceTestKotlinJs.dceOptions.devMode = true

configurations {
    ktlint
}

repositories {
    mavenCentral()
    jcenter()
    maven { url("https://dl.bintray.com/korlibs/korlibs") }
}

compileKotlin2Js {
    kotlinOptions.sourceMap = true
    kotlinOptions.sourceMapEmbedSources = "always"
    // remaining configuration options
    kotlinOptions.metaInfo = true
    kotlinOptions.moduleKind = 'commonjs'
}

task ktlint(type: JavaExec) {
    description = "Check Kotlin code style."
    main = "com.github.shyiko.ktlint.Main"
    classpath = configurations.ktlint
    args "--verbose", "src/**/*.kt"
}

check.dependsOn ktlint

task ktlintFormat(type: JavaExec) {
    description = "Fix Kotlin code style deviations."
    main = "com.github.shyiko.ktlint.Main"
    classpath = configurations.ktlint
    args "-F", "src/**/*.kt"
}

dokka {
    outputFormat = 'html'
    outputDirectory = "doc"
}

dependencies {
    ktlint "com.github.shyiko:ktlint:$ktlint_version"
    compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
    implementation "com.soywiz.korlibs.korio:korio-js:$korioVersion"
    implementation "com.soywiz.korlibs.klogger:klogger-js:$kloggerVersion"
}

这是创建的文件venus.js

更多背景信息:是我正在进行的项目。我正在尝试添加一个外部虚拟内存,并且与内存的通信应该通过WebSockets

标签: kotlingradlebuild.gradlekotlin-multiplatformkotlin-js

解决方案


推荐阅读