JetBrains/kotlin-wrappers

Uncaught ReferenceError: module is not defined

McPringle opened this issue ยท 9 comments

When I include kotlin-extensions in my HTML page, I get this error in all browsers:

Uncaught ReferenceError: module is not defined
    at kotlin-extensions.js:93
(anonymous) @ kotlin-extensions.js:93

This is line 93:

}(module.exports, require('kotlin'), require('core-js/library/fn/object/assign')));

The kotlin module is loaded, but core-js seems new to me. I could not find any documentation in the kotlin-extensions project which mentions core-js. As I include kotlin-extensions via gradle and I can not find anything core-js releated in the project, I assume the kotlin-extensions artifact is missing the dependency to core-js.

dependencies {
    compile(
        "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion",
        "org.jetbrains:kotlin-extensions:$kotlinExtensionsVersion",
        "org.jetbrains:kotlin-react:$kotlinReactVersion",
        "org.jetbrains:kotlin-react-dom:$kotlinReactVersion"
    )
}

core-js is an npm dependency of @jetbrains/kotlin-react-dom package, so you may need to run npm install in its directory

Hi @Hypnosphi

Thank you very much for your answer. The require comes from the kotlin-extensions source code. I can't run a npm install because there is no npm configuration in the project. I use gradle to manage the dependencies and to build the project (see bintray.com). If core-js is a dependency of kotlin-extensions I would assume that gradle is going to download that, too. But in fact it is not downloading core-js, so I assume that this dependency is not mentioned in the artifact metadata.

Please take a look at the pom of kotlin-extensions:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.jetbrains</groupId>
  <artifactId>kotlin-extensions</artifactId>
  <version>1.0.0-pre.19-kotlin-1.2.20</version>
  <dependencies>
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-stdlib-js</artifactId>
      <version>1.2.20</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.jetbrains.kotlinx</groupId>
      <artifactId>kotlinx-html-js</artifactId>
      <version>0.6.8</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

The dependency to core-js is missing there. The core-js library is used in Object.kt line 39:

@JsModule("core-js/library/fn/object/assign")
external fun <T, R : T> assign(dest: R, src: T): R

@Leonya how do you handle this issue?

@Hypnosphi we have it added into the project package.json

Is it possible that this issue still exists?

Gradle:

compile 'org.jetbrains:kotlin-extensions:1.0.1-pre.28-kotlin-1.2.30'

And the error:

Uncaught ReferenceError: module is not defined
    at kotlin-extensions.js:111

The line is this:

}(module.exports, require('kotlin'), require('core-js/library/fn/object/assign')));

It does exist, you have to npm install core-js. If you're not using npm we don't have a solution at the moment, I'm afraid.

It sucks that this cannot yet be solved without npm.
I started using the frontend plugin to do my multiplatform build with a kotlin-react frontend: https://github.com/Kotlin/kotlin-frontend-plugin
That way you can wrap npm with gradle and your build.gradle and install all dependencies (including core-js) locally. And you get Webpack bundling for free if you use the plugin, meaning the end result will be a single js file (that is served by ktor in my case).

Still, it's definitely less than optimal that we need to use npm for even simple Kotlin projects thanks to this issue. Maybe it makes sense to release a "fat" extensions-file that includes core-js.

I'm going to drop the core-js import. Make sure that you provide your own Object.assign polyfill if you're using objectAssign and have to support IE11