Creating and Testing a CoffeeScript ES6‑like Class in Node.js
Step‑by‑step guide to define a CoffeeScript class, compile it to JavaScript, and verify its behavior in Node.js with correct this binding.
20 May 2026, 01:27 UTC

Desired outcome
Define a simple class in CoffeeScript, compile it to plain JavaScript, and verify that the compiled module works correctly in a Node.js environment, including proper this binding in instance methods.
Prerequisites
- Node.js version 12 or newer installed (
node --version). - CoffeeScript installed globally via npm (
npm install -g coffeescript). - A terminal or command prompt with access to the
nodeandcoffeeexecutables.
Procedure
- Create the CoffeeScript class file.
# Person.coffee class Person constructor: (@name, @age) -> greet: -> "Hello, I am #{@name} and I am #{@age} years old." # Using fat arrow to lexically bind `this` for callbacks yearlyUpdate: (callback) -> setTimeout () => @age += 1 callback @age , 100 # Export for Node.js module.exports = PersonSave this as
Person.coffeein an empty project folder. - Compile to JavaScript.
coffee -c Person.coffeeThis generates
Person.jsin the same directory. - Inspect the generated JavaScript (optional but recommended).
Open
Person.jsand verify that the class is transpiled to a constructor function with methods placed on its prototype, and that the fat arrow (=>) produces a lexicalthisbinding inside thesetTimeoutcallback. - Create a test script.
// test.js const Person = require('./Person'); const p = new Person('Ada', 30); console.log(p.greet()); p.yearlyUpdate((newAge) => { console.log(`After update: age is ${newAge}`); });Save as
test.js. - Run the test with Node.js.
node test.jsObserve the console output.
Expected checks
- The compiled
Person.jsshould contain a constructor function namedPersonand prototype methodsgreetandyearlyUpdate. - Running
node test.jsshould print a greeting line similar to: - "Hello, I am Ada and I am 30 years old."
- After approximately 100 ms, a second line should appear indicating the updated age, e.g.,
After update: age is 31. - If the output matches the expected pattern, the class and its methods are correctly bound and executed.
Recovery options
- Indentation errors. CoffeeScript is whitespace‑sensitive. If compilation fails, re‑check that each level of indentation uses spaces (or tabs) consistently and that no stray whitespace appears at the start of lines.
- Incorrect
thisbinding. Ensure that callbacks needing the instance use the fat arrow (=>) rather than the thin arrow (->). Switching to=>will bindthislexically to the class instance. - Node version incompatibility. The generated JavaScript uses ES2015 features (e.g.,
let, arrow functions). If you run an older Node (< 4) that lacks support, either upgrade Node or transpile the output further with a tool like Babel. - Cleaning generated files. To start over, delete
Person.js(and any*.mapfiles if you generated source maps) and repeat the compile step.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.