Assuming the Kotlin compiler puts the created JS file (say server.js
) into the default location at build/classes/kotlin/main
and the resource file (file.json
) into build/resources/main
.
And you are running server.js
by executing node build/classes/kotlin/main/server.js
According to the NodeJS documentation:
Local modules and JSON files can be imported using a relative path (e.g. ./, ./foo, ./bar/baz, ../foo) that will be resolved against the directory named by __dirname (if defined) or the current working directory.
(https://nodejs.org/api/modules.html#modules_require_id)
In our case __dirname
is build/classes/kotlin/main
So the correct require statement is:
val serviceAccount = js("require('../../../resources/main/file.json')")
or if require
is defined as a Kotlin function like in the question
val serviceAccount = require("../../../resources/main/file.json")
json-loader
plugin for webpack to load json with code – Aday