Docker Community Forums

Share and learn in the Docker community.

How to integrate debugger for Node,js scripts written using Kotlin in IntelliJ Idea


(Aseredenko) #1

Hello,

As you might know Kotlin allows developers write code which converts to JS code. Obviously, developers can create Node.js application as well.

I have implemented Kotlin Node.js demo using this tutorial - https://medium.com/@Miqubel/your-first-node-js-app-with-kotlin-30e07baa0bf7

And created a Dockerfile using this tutorial - https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ + added java to make Gradle work so the final Dockerfile looks like:

    FROM node:carbon

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

# add webupd8 repository
RUN \
    echo "===> add webupd8 repository..."  && \
    echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee /etc/apt/sources.list.d/webupd8team-java.list  && \
    echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee -a /etc/apt/sources.list.d/webupd8team-java.list  && \
    apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886  && \
    apt-get update  && \
    \
    \
    echo "===> install Java"  && \
    echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections  && \
    echo debconf shared/accepted-oracle-license-v1-1 seen true | debconf-set-selections  && \
    DEBIAN_FRONTEND=noninteractive  apt-get install -y --force-yes oracle-java8-installer oracle-java8-set-default  && \
    \
    \
    echo "===> clean up..."  && \
    rm -rf /var/cache/oracle-jdk8-installer  && \
    apt-get clean  && \
    rm -rf /var/lib/apt/lists/*

EXPOSE 8080
CMD ["./gradlew", "build"]
CMD [ "node", "web/output.js" ] 

build.gradle

group 'org.example'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.2.30'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin2js'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}

compileKotlin2Js {
    kotlinOptions.outputFile = "${projectDir}/web/output.js"
    kotlinOptions.moduleKind = "commonjs"
    kotlinOptions.sourceMap = true
}

Setting up a debugger in IntelliJ Idea without Docker is super easy:

  1. Create a new configuration for Node.js
  2. Type your working directory
  3. Type a path to JavaScript file (in my case web/output.js)
  4. Save and click “Debug selected configuration”

Is it possible somehow to attach remote debug for Kotlin in Docker?