10
Stack Exchange
votes

Using ESM Modules with Coffeescript and Node.js

Posted on 12/12/2019
UQ ID:392
Technology
Stack Overflow
Original

Ecmascript Modules are the future for packaging JS code, and both Node.js and Coffeescript support them. But I've had some trouble getting their support for ESM to work together.

The current stable Node (12.x) has ESM modules behind a flag (--experimental-modules). Coffeescript supports passing flags through to Node with --nodejs. So with a couple of files using ESM modules:

# a.coffee
import b from './b.coffee'

b()



# b.coffee
b = ->
  console.log "Hello"

export default b

In theory, we can run this code with npx coffee --nodejs --experimental-modules a.coffee. In practice this raises an error:

13:24 $ npx coffee --nodejs --experimental-modules a.coffee
(node:8923) ExperimentalWarning: The ESM module loader is experimental.
(node:8923) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/media/projects/coffeemodules/a.coffee:1
import b from './b.coffee';
^^^^^^

SyntaxError: Cannot use import statement outside a module
...

The error and docs say there are two ways to flag a file as containing an ESM module, one is to use the mjs extension (which isn't available to us here), and the other is to set "type": "module" in package.json, which also doesn't seem to work.

So: can it be done? Is there a way to get Coffeescript and Node.js ES modules to play together?

Keywords & Tags
node.js
coffeescript
es6-modules
Model Answers (9)
Submitted answers from various AI models
Unsolved
No reviews yet

K2-Think

Pending UQ Validation
Needs Human Verification
MBZUAI-IFM
Qwen2.5-32B
Sep 18, 2025, 01:50 PM

To use ECMAScript Modules (ESM) with CoffeeScript and Node.js, follow these steps:

1. Compile CoffeeScript to ESM JavaScript

CoffeeScript needs to transpile your ESM syntax (import/export) into JavaScript without converting it to CommonJS. Use the --esm flag during compilation to preserve ESM statements.

2. Adjust Import Paths

Node.js's ESM loader only recognizes .js, .mjs,...

0 model reviews
0 human reviews
No reviews yet

GPT-5

Failed UQ Validation
Needs Human Verification
OpenAI
Sep 2, 2025, 06:56 AM

Short answer: not directly with the coffee runner. You need to compile to JS first (or use a custom ESM loader). Here’s why and how.

Why your command fails

  • The coffee CLI does not execute your file via Node’s ESM loader. It compiles to JS and evaluates it as a CommonJS script, so Node treats the code as CJS and throws “Cannot use import statement outside a module.”
  • Even if the entry file were...
0 model reviews
0 human reviews
No reviews yet

o4 mini

Failed UQ Validation
Needs Human Verification
OpenAI
Aug 1, 2025, 12:00 AM

Node’s new ES‐module loader simply doesn’t know how to handle “.coffee” files (and it will never invoke the CoffeeScript‐require hook), so there is no magic switch in Node 12 to make your ​.coffee files load as native ESM. The only two ways to get “real” ES modules out of CoffeeScript today are:

  1. Pre-compile your ​.coffee → ​.mjs (or ​.js in a "type":"module" package) and then run Node on t...
0 model reviews
0 human reviews
No reviews yet

o3 Pro

UQ Validated
Needs Human Verification
OpenAI
Aug 1, 2025, 12:00 AM

Short answer
At the moment you cannot run CoffeeScript source files directly with node --experimental-modules and have them behave as native ES modules. The CoffeeScript command-line tool works by installing a “require hook” that on-the-fly compiles the .coffee file and then lets Node execute the resulting JavaScript as a CommonJS module.
The ES-module loader that Node exposes under `-...

0 model reviews
0 human reviews
No reviews yet

Claude 3.7 Sonnet

Failed UQ Validation
Needs Human Verification
Anthropic
Aug 1, 2025, 12:00 AM

Using ESM Modules with CoffeeScript and Node.js

There are several ways to get CoffeeScript and Node.js to work with ES modules. Here are the best approaches:

Solution 1: Compile First Approach

The most reliable approach is to compile your CoffeeScript files to JavaScript first, and then run them with Node:

  1. Compile your CoffeeScript files:
    npx coffee -c a.coffee b.coffee
    

...

0 model reviews
0 human reviews
No reviews yet

DeepSeek R1

Failed UQ Validation
Needs Human Verification
DeepSeek
deepseek-ai/DeepSeek-R1
Aug 1, 2025, 12:00 AM
<think> We are trying to run CoffeeScript files as ES modules in Node.js (version 12.x, which requires the `--experimental-modules` flag for ESM support). The problem is that even when passing the flag via CoffeeScript's `--nodejs` option, Node.js does not recognize the `.coffee` files as ES modules and throws an error.

The error message suggests two ways to enable ESM:

  1. Use the .mjs extensio...
0 model reviews
0 human reviews
No reviews yet

o3

Failed UQ Validation
Needs Human Verification
OpenAI
Aug 1, 2025, 12:00 AM

Short version
Node’s experimental E-S-M loader only knows how to read JavaScript files that end in “.mjs” (or in “.js” when a package.json contains "type": "module").
It cannot be persuaded to run any other file-extension and it gives no hook that would let CoffeeScript be compiled on the fly.
The CoffeeScript runner, on the other hand, feeds the code to Node through the Common-JS `require...

0 model reviews
0 human reviews
No reviews yet

Claude Opus 4

Failed UQ Validation
Needs Human Verification
Anthropic
Aug 1, 2025, 12:00 AM

Yes, you can get CoffeeScript and Node.js ES modules to work together, but there are some specific configurations needed. The issue you're encountering is that CoffeeScript needs to compile the code first, and Node.js needs to recognize the compiled output as ES modules.

Here are the solutions:

Solution 1: Using package.json with "type": "module"

  1. Create a package.json file with the modu...
0 model reviews
0 human reviews
No reviews yet

Gemini 2.5 Pro

Failed UQ Validation
Needs Human Verification
Google
Aug 1, 2025, 12:00 AM

Excellent question. You've correctly identified the core issue: the way CoffeeScript's traditional Node.js integration works is fundamentally incompatible with how Node's ESM loader works.

Here’s a breakdown of why your attempt failed and two modern solutions to get it working.

The Core Problem: CJS vs. ESM Loaders

Your command coffee --nodejs ... uses CoffeeScript's traditional registrat...

0 model reviews
0 human reviews
Submit Your Model's Answer
Your submission will be validated by our LLM validator and then by human experts

You need to be logged in to submit an answer