{"library":"rechoir","title":"rechoir: Node Environment File Loader Preparer","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install rechoir"],"cli":{"name":"rechoir","version":null}},"imports":["const rechoir = require('rechoir');","const rechoir = require('rechoir');\nrechoir.prepare(config, filepath);","const config = require('interpret').extensions;"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const fs = require('fs');\nconst path = require('path');\nconst config = require('interpret').extensions;\nconst rechoir = require('rechoir');\n\n// Create dummy files for demonstration\nconst fixtureDir = path.join(__dirname, 'fixtures');\nfs.mkdirSync(fixtureDir, { recursive: true });\nfs.writeFileSync(path.join(fixtureDir, 'test.coffee'), 'console.log(\"Hello from CoffeeScript!\");');\nfs.writeFileSync(path.join(fixtureDir, 'test.toml'), '[section]\\nkey = \"value\"');\nfs.writeFileSync(path.join(fixtureDir, 'test.js'), 'module.exports = { js: true };');\n\n// NOTE: For .coffee and .toml, you would typically need to install\n// `coffee-script` and `toml` packages in your project.\n// For this example to run without errors, ensure they are installed or comment out those lines.\n\ntry {\n  rechoir.prepare(config, path.join(fixtureDir, 'test.coffee'));\n  console.log('CoffeeScript file loaded via prepare, attempting require...');\n  require(path.join(fixtureDir, 'test.coffee')); // This will execute the CoffeeScript\n} catch (e) {\n  console.error(`Error preparing/requiring CoffeeScript: ${e.message}. Did you install 'coffee-script'?`);\n}\n\ntry {\n  rechoir.prepare(config, path.join(fixtureDir, 'test.toml'));\n  console.log('TOML file loaded via prepare, attempting require...');\n  const tomlConfig = require(path.join(fixtureDir, 'test.toml'));\n  console.log('TOML content:', tomlConfig); // This will show the parsed TOML\n} catch (e) {\n  console.error(`Error preparing/requiring TOML: ${e.message}. Did you install 'toml'?`);\n}\n\n// Standard JS files don't need prepare\nconst jsConfig = require(path.join(fixtureDir, 'test.js'));\nconsole.log('JavaScript content:', jsConfig);\n\n// Cleanup (optional)\n// fs.unlinkSync(path.join(fixtureDir, 'test.coffee'));\n// fs.unlinkSync(path.join(fixtureDir, 'test.toml'));\n// fs.unlinkSync(path.join(fixtureDir, 'test.js'));\n// fs.rmdirSync(fixtureDir);","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}