rechoir: Node Environment File Loader Preparer

0.8.0 · active · verified Sun Apr 19

rechoir is a utility designed to prepare the Node.js `require` environment for dynamically loading files with various non-native extensions. It operates by registering module loaders into `require.extensions`, leveraging `interpret`-like configuration objects to map file extensions to their respective transpiler or loader modules. Currently stable at version 0.8.0, releases are typically feature-driven and occur on an irregular cadence, often in conjunction with updates within the broader Gulp.js ecosystem where it serves as a dependency for tools like `liftoff`. A key differentiator is its ability to integrate with third-party transpilers (e.g., CoffeeScript, TypeScript, Babel) without bundling them, instead requiring developers to install these transpiler modules as local project dependencies. This design makes rechoir a flexible tool for command-line interfaces and build systems that need to process files written in languages other than standard JavaScript directly via Node's module resolution system.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use rechoir with `interpret.extensions` to dynamically register and load files with custom extensions like `.coffee` and `.toml` into the Node.js runtime, allowing them to be `require()`-d like standard JavaScript modules. It highlights the need for local transpiler installations.

const fs = require('fs');
const path = require('path');
const config = require('interpret').extensions;
const rechoir = require('rechoir');

// Create dummy files for demonstration
const fixtureDir = path.join(__dirname, 'fixtures');
fs.mkdirSync(fixtureDir, { recursive: true });
fs.writeFileSync(path.join(fixtureDir, 'test.coffee'), 'console.log("Hello from CoffeeScript!");');
fs.writeFileSync(path.join(fixtureDir, 'test.toml'), '[section]\nkey = "value"');
fs.writeFileSync(path.join(fixtureDir, 'test.js'), 'module.exports = { js: true };');

// NOTE: For .coffee and .toml, you would typically need to install
// `coffee-script` and `toml` packages in your project.
// For this example to run without errors, ensure they are installed or comment out those lines.

try {
  rechoir.prepare(config, path.join(fixtureDir, 'test.coffee'));
  console.log('CoffeeScript file loaded via prepare, attempting require...');
  require(path.join(fixtureDir, 'test.coffee')); // This will execute the CoffeeScript
} catch (e) {
  console.error(`Error preparing/requiring CoffeeScript: ${e.message}. Did you install 'coffee-script'?`);
}

try {
  rechoir.prepare(config, path.join(fixtureDir, 'test.toml'));
  console.log('TOML file loaded via prepare, attempting require...');
  const tomlConfig = require(path.join(fixtureDir, 'test.toml'));
  console.log('TOML content:', tomlConfig); // This will show the parsed TOML
} catch (e) {
  console.error(`Error preparing/requiring TOML: ${e.message}. Did you install 'toml'?`);
}

// Standard JS files don't need prepare
const jsConfig = require(path.join(fixtureDir, 'test.js'));
console.log('JavaScript content:', jsConfig);

// Cleanup (optional)
// fs.unlinkSync(path.join(fixtureDir, 'test.coffee'));
// fs.unlinkSync(path.join(fixtureDir, 'test.toml'));
// fs.unlinkSync(path.join(fixtureDir, 'test.js'));
// fs.rmdirSync(fixtureDir);

view raw JSON →