Coffee-Script Connect/Express Middleware

raw JSON →
0.2.1 verified Thu Apr 23 auth: no javascript abandoned

iced-coffee-middleware (also known as `coffee-middleware`) is a Connect/Express middleware designed for on-the-fly compilation of CoffeeScript files (`.coffee`) into JavaScript. Currently at version 0.2.1, it provides functionality to dynamically serve compiled JavaScript, offering options such as `force` for continuous recompilation, `bare` for compilation without the top-level safety wrapper, and `encodeSrc` for embedding base64 source maps. This package leverages patterns from `less-middleware` and is built for integration with older `connect` and `express` versions that utilized `createServer()` and `app.configure()`. Due to its reliance on these long-deprecated APIs and the declining prevalence of CoffeeScript in new web development, the package's active maintenance and applicability to modern Node.js ecosystems are highly questionable. Its development cadence is effectively halted, with no significant updates in many years, making it primarily a historical artifact rather than a viable solution for contemporary projects.

error TypeError: app.createServer is not a function
cause Attempting to use `express.createServer()` with Express 4.x or later.
fix
This middleware is designed for Express 3.x. You either need to downgrade Express (e.g., npm install express@3.x) or refactor your application to not use this legacy middleware.
error Error: Cannot find module 'coffee-middleware'
cause The package is not installed or the Node.js module resolution path is incorrect.
fix
Ensure the package is installed: npm install coffee-middleware (or npm install iced-coffee-middleware if that was the intended name, though the readme uses coffee-middleware). Check node_modules and your package.json.
error SyntaxError: Cannot use import statement outside a module
cause Attempting to `import` `coffee-middleware` in a CommonJS context or using `require()` in an ESM context without proper configuration.
fix
This package is CommonJS. Use const coffeeMiddleware = require('coffee-middleware'); and ensure your file is treated as CommonJS (e.g., no "type": "module" in package.json or rename to .cjs).
breaking This package relies on `express.createServer()` and `app.configure()` which were deprecated in Express 4.x. It is not compatible with modern Express applications without significant bridging or downgrading Express versions.
fix Consider using modern build tools (e.g., Webpack, Rollup, Vite) to precompile CoffeeScript (or switch to TypeScript/modern JS) or use an Express 3.x compatible environment for this middleware.
deprecated The use of CoffeeScript for new web development projects has largely declined. Modern projects typically use TypeScript or contemporary JavaScript with transpilers like Babel.
fix Evaluate if CoffeeScript is still the appropriate language for your project. Consider migrating to TypeScript or plain JavaScript with a modern build setup for better tooling and community support.
gotcha This package is CommonJS-only and cannot be directly `import`ed in an ESM module context. Attempting to do so will result in a runtime error.
fix Ensure your project is configured for CommonJS modules if using this package, or use `require()` within an ESM module if supported by your runtime/transpiler setup (e.g., Node.js with `createRequire`).
gotcha The package is at version 0.2.1, indicating an early development stage and potential lack of stability or extensive testing for production environments. It has not seen updates for many years.
fix Use with caution in production. Review the source code for potential vulnerabilities or unhandled edge cases. Consider if a more mature and maintained solution exists for your needs.
npm install iced-coffee-middleware
yarn add iced-coffee-middleware
pnpm add iced-coffee-middleware

This quickstart demonstrates how to set up `coffee-middleware` with an older Express 3.x application to compile and serve CoffeeScript files on demand, including a basic example `.coffee` file and a server setup.

const express = require('express');
const coffeeMiddleware = require('coffee-middleware');
const path = require('path');
const fs = require('fs');

// Create a dummy public directory and a .coffee file for demonstration
const publicDir = path.join(__dirname, 'public');
const coffeeFilePath = path.join(publicDir, 'hello.coffee');

if (!fs.existsSync(publicDir)) {
  fs.mkdirSync(publicDir);
}
fs.writeFileSync(coffeeFilePath, 'console.log "Hello from CoffeeScript!"');

// Note: express.createServer() and app.configure() are deprecated in modern Express.
// This example uses patterns from Express 3.x to demonstrate functionality.
const app = express();

// Mock the old `configure` method for compatibility with the example structure
app.configure = function(fn) { fn(); }; 

app.configure(function () {
  app.use(coffeeMiddleware({
    src: publicDir,
    bare: true, // Compile without the top-level safety wrapper
    compress: true, // Example option (may require 'uglify-js' in older versions)
    force: true // Ensure recompilation on each request for demo
  }));
  // Serve static files, ensuring compiled JS files can be accessed
  app.use(express.static(publicDir));
});

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
  console.log(`Try accessing http://localhost:${PORT}/hello.js`);
  console.log(`(This will compile and serve public/hello.coffee as JavaScript)`);
});

// To run this example, you would typically need to install older versions:
// npm install express@3.x coffee-middleware connect@2.x