Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

454
Views
Load resource file (json) in kotlin js

Given this code, where should I place the file.json to be able to be found in the runtime?

// path: src/main/kotlin/Server.kt
fun main() {
  val serviceAccount = require("file.json")
}

I tried place it under src/main/resources/ without luck. I also use Gradle to compile kotlin to js with kotlin2js plugin.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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") 
over 4 years ago · Santiago Trujillo Report

0

You may just use js("require('./file.json')") if you do not have import for the require function in Kotlin. The result will be dynamic, so you may cast it to Map.

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/js.html

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!