With grooscript, you can convert your groovy code to javascript. Main use is associated to web applications running in your browsers, in the client side. But maybe, you want to use in the server side, in a Node.js application. I think node isn’t the best idea in your server side, but has very good things, as a very big community and lot of modules. Also, that application runs very fast.

The source code of this example is part of grooscript demos project on github. You need node.js, and some npm modules. Only grooscript module is needed to use grooscript converted code, rest of modules are used for fun in this example. Install node.js, then install modules, maybe with sudo:

> npm install

Also you need grooscript gradle plugin to convert groovy code to javascript. Install gradle if you don’t have it installed in your machine. You can use Gvm to install gradle 2.5.


Is a node.js project, but we will use gradle to work with groovy files and convert to javascript.

plugins {
    id "org.grooscript.conversion" version "1.2.0" (1)
    id "org.asciidoctor.convert" version "1.5.2"

apply plugin: 'idea'
apply plugin: 'groovy'

ext {
    groovyVersion = '2.4.4'
    gradleCurrentVersion = '2.5'
    grooscriptVersion = '1.2.0'
    imagesDir = 'img'
    docImagesDir = 'img'
    jsDir = 'js'

repositories {

dependencies { (2)
    compile group: 'org.codehaus.groovy', name: 'groovy', version: groovyVersion
    compile group: 'org.codehaus.groovy', name: 'groovy-json', version: groovyVersion
    compile group: 'org.codehaus.groovy', name: 'groovy-test', version: groovyVersion
    compile('io.reactivex:rxgroovy:1.0.0') {
        exclude module: 'groovy-all'
    compile ("org.grooscript:grooscript:${grooscriptVersion}") {
        exclude module: 'groovy'
        exclude module: 'groovy-json'
    testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
        exclude module: 'groovy-all'
    testCompile 'cglib:cglib-nodep:2.2.2'

grooscript {
    source = ['src/main/groovy/Execute.groovy',
              'src/main/groovy/colors/JsColors.groovy'] (3)
    destination = 'js' (4)
    classpath = ['src/main/groovy'] (5)
    initialText = '//Grooscript converted file'
1 Get grooscript gradle plugin from bintray.
2 As a normal groovy proyect we will use groovy and some grooscript features.
3 Groovy file to be converted to javascript.
4 Folder to save js file.
5 Classpath to use when compiling groovy file before convert it.

To convert Execute.groovy to javascript, do > ./gradlew convert. There are more task, to explore the plugin take a look at starting guide.


Now, let’s take a look to colors.js code. The Node.js application that you run with node:

var fs = require('fs');
var gs = require('grooscript'); (1)

eval(fs.readFileSync('js/JsColors.js')+''); (2)
eval(fs.readFileSync('js/NodeJs.js')+''); (2)
eval(fs.readFileSync('js/Execute.js')+''); (2)
1 Grooscript module, needed if you use groovy converted code in your application.
2 The javascript converted file with the code of our application.


Grooscript support a lot of groovy magic, as traits, dsl’s, lists, …​ This application play a bit with some Node.js modules using a bit of that magic. You are creating your application in groovy, and you can use the tools you know, as Spock, Codenarc, Gradle,…​ always better than use javascript :)

import static nodejs.NodeJs.nodejs

nodejs {
    rainbow '\nGrooscript in action 2015!' (1)
    red     '--------------------------\n'

    module 'request' (2)
    module 'async'

    parallel([ (3)
        { countBodyChars 'http://grails.org' },
        { countBodyChars 'http://bintray.com' },
        { countBodyChars 'http://google.com' },
        { countBodyChars 'http://twitter.com' },
        { countBodyChars 'http://gradle.org' },
        { bold '\nReading url\'s...\n' },
1 Color output in your console using colors.js module.
2 Add node.js modules.
3 Execute tasks in parallel using async.js module.
package nodejs

import colors.JsColors
import org.grooscript.asts.GsNative

class NodeJs implements JsColors { (1)

    @GsNative (2)
    def module(String name) {/*
        try {
            global[name] = require(name);
        } catch(err) {
            this.red('Module '+name+' not installed, please install it.');

    NodeJs() {
        module 'colors' (3)

    def countBodyChars(String url) {
        def time = new Date()
        grey "  Going $url"
        request(url, { error, response, body -> (4)
            if (!error && response.statusCode == 200) {
                println "$url body size: ${body.size()} time: ${new Date().time - time.time}"
            } else {
                println "Error: ${error}"

    def parallel(List<Closure> closures) {
        async.parallel closures (5)

    static nodejs(@DelegatesTo(NodeJs) Closure cl) { (6)
        def node = new NodeJs()
        cl.delegate = node
1 Out groovy class implements a trait.
2 This AST will put in your js method the code between /* and */
3 Add colors module in constructor, because color functions comes with the trait.
4 Request comes from request module.
5 Use async module to make a parallel execution of groovy closures that will be javascript functions at runtime.
6 You use groovy features in your code as @DelegateTo, to get help from your IDE writing the DSL.

Finally the trait with the functions to color our console info JsColors.groovy:

package colors

import org.grooscript.asts.GsNative

trait JsColors {
    def red(String msg) {/*

    def bold(String msg) {/*

    def grey(String msg) {/*

    def rainbow(String msg) {/*

All this groovy code is converted to javascript and will run in your Node.js application.

Run it

If your groovy code is converted to javascript, a file js/Execute.js must exist. To convert Execute.groovy use > ./gradlew convert. If some error, as compilation error, or conversion failure, you get info in the console. If all is ok, just run the application with > node js/colors.js

This application use some nice Node.js modules, as show console messages in console, get content from url’s, or launch tasks concurrently.



This is not the best example, but you can take the idea, how to interact between Node.js and groovy. Groovy can do the same things that node, but maybe you have to use javascript(sorry) and you can introduce Groovy magic to make your javascript life more fun.

Please, if you have comments, suggestions, problems,…​ don’t hesitate to contact me at grooscript@gmail.com, or open an issue or feature in Github. This is an open source project and your feedback is more than welcome to improve it. More guides come in the future, also can find more documentation in grooscript site.