Using Google Closure Compiled Code for AWS Lambda Functions

clojurescript, javascript

Recently, I tried to get a development environment set up for developing a single page web application ClojureScript - but with a backend written to run on AWS Lambda functions, also in ClojureScript.

ClojureScript seems to use the Google Closure Compiler for compiling JavaScript. The Google Closure Compiler uses its own module and import/export/require system that is different from Node’s module system.

Because AWS Lambda expects your JS to export the handler functions for the Lambda function using the Node module system, I ended up with a real problem:

How do you require Google Closure-compiled ClojureScript into a node module?

The answer ended up being quite simple. Given that Closure is creating a compiled file with name functions.js, which in turn is exporting a namespace functions.core, you can create a new main.js file in the same directory as the compiled functions.js file with the following content:

1
2
var f = require("./functions.js");
module.exports = f.functions.core;

This took me longer than I care to admit to figure out, though that’s probably mostly a function of how little I deal with JavaScript module systems. Since thanks go to Matthew Stump’s post Writing NodeJS Modules in ClojureScript, which had the trick as part of it.


I'm looking for better ways to build software businesses. Find out if I find something.