19

Currently trying to get a Kotlin "Hello, World" to compile to JS via the command line. I've followed the tutorial:

https://kotlinlang.org/docs/tutorials/javascript/getting-started-command-line/command-line-library-js.html

I'm seeing the Javascript files being generated, but I'm missing the kotlin.js file that I would expect to see per: https://kotlinlang.org/docs/tutorials/javascript/kotlin-to-javascript/kotlin-to-javascript.html

The first few lines of the generated JS files read: if (typeof kotlin === 'undefined') { throw new Error("Error loading module 'sample-library'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'sample-library'."); }

so it's clear that the it's meant to be run with a file that instantiates kotlin. Any ideas why I'm not seeing it? I'm following the tutorial exactly as written. I'm using the latest version of the compiler from homebrew, which is 1.1.2.2

guyIntrepid
  • 548
  • 5
  • 8

1 Answers1

14

As described here, yes, you'll need to include kotlin.js before you can run your own Kotlin code. This file contains the Kotlin runtime and standard library.

If you're doing this from the command line, you can find kotlin.js in the lib folder of the compiler, inside kotlin-stdlib-js.jar (which you can just open as a regular .zip file).

zsmb13
  • 85,752
  • 11
  • 221
  • 226
  • 4
    Aha! there it is. Thank you so much. Note to other Node.js folks, you'll have to add `kotlin = require('./kotlin.js')` somewhere before your compiled JS runs. – guyIntrepid May 24 '17 at 11:29
  • 1
    ^ or better yet, compile with `-module-kind commonjs` after installing the kotlin dependency from npm. – guyIntrepid May 24 '17 at 11:37
  • 2
    @guyIntrepid that solved my issue! I'm building with IntelliJ, any reason you can think of why it wouldn't automatically add that in as a part of the build? :/ – Robbie Cronin Dec 28 '18 at 01:49
  • @guyIntrepid. This should be the correct answer. Someone beginning in the language should not have to decompress the runtime jars to get at the kotlin.js file, IMHO. – tutiplain Apr 18 '21 at 17:50
  • But yes, thumbs up tp @zsmb13, too. This is not obvious from the CLI tutorial. – tutiplain Apr 18 '21 at 17:50