{"id":25946,"library":"mocha-image-compiler","title":"Mocha Image Compiler","description":"Mocha compiler that enables webpack-like importing of images (PNG, JPG, GIF, SVG) in Node.js test environments. Current version 1.0.0 is stable and rarely updated. It works by replacing image require calls with a simple path string, making it ideal for testing components that import images. Unlike webpack loaders, this is a lightweight, no-config solution specifically designed for Mocha's --compilers flag. Requires Mocha as a peer dependency.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/SteveWilshaw/mocha-image-compiler","tags":["javascript","mocha"],"install":[{"cmd":"npm install mocha-image-compiler","lang":"bash","label":"npm"},{"cmd":"yarn add mocha-image-compiler","lang":"bash","label":"yarn"},{"cmd":"pnpm add mocha-image-compiler","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Runs as a Mocha compiler; Mocha is required as a peer dependency.","package":"mocha","optional":false}],"imports":[{"note":"Used as a Mocha compiler, not imported in test code. The 'require' pattern is typical for Mocha config, but direct usage inside tests is not needed.","symbol":"mocha-image-compiler","correct":"require('mocha-image-compiler') (CommonJS) or import 'mocha-image-compiler' (ESM wrapper); used via --compilers flag, not directly imported in test files"}],"quickstart":{"code":"npm install --save-dev mocha mocha-image-compiler\necho \"var logo = require('./logo.png'); console.log(logo);\" > test.js\necho '{}' > package.json\nmocha --compilers .:mocha-image-compiler test.js","lang":"javascript","description":"Installs mocha and mocha-image-compiler, creates a test file that imports a PNG image, and runs mocha with the compiler to replace image require with a path string."},"warnings":[{"fix":"Use only in Mocha test environment; for bundlers use appropriate loaders (e.g., file-loader).","message":"Only works with Mocha's --compilers flag; does not support webpack or other bundlers.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Assume require('./image.png') returns a string path; do not expect a Buffer or URL object.","message":"Returns the file path as a string, not the actual image data or a module. This may differ from webpack behavior where require returns a URL or data URI.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Install mocha alongside mocha-image-compiler: npm install --save-dev mocha","message":"Requires Mocha to be installed separately; not included as a dependency.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use only static string paths in require(); do not use variables or expressions.","message":"Does not support dynamic require() calls; only static require paths are transformed.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Specify compilers in correct order: mocha --compilers js:babel/register,.:mocha-image-compiler","message":"May conflict with other Mocha compilers if not ordered correctly (e.g., with babel/register).","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Run npm install --save-dev mocha-image-compiler in your project.","cause":"Package not installed globally or missing in node_modules.","error":"Error: Cannot find module 'mocha-image-compiler'"},{"fix":"Use --compilers .:mocha-image-compiler (dot colon package-name). Ensure the dot matches the file extension (e.g., . for any extension, or .js:...).","cause":"Incorrect compiler syntax in --compilers flag.","error":"Error: compiler not found: mocha-image-compiler"},{"fix":"Add --experimental-modules or use import statements. Alternatively, set 'type': 'commonjs' or use .cjs extension.","cause":"Mocha is running with ESM ('type': 'module' in package.json) but using require() syntax.","error":"ReferenceError: require is not defined in ES module scope"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}