{"id":15668,"library":"karma-commonjs","title":"Karma CommonJS Plugin","description":"The `karma-commonjs` plugin facilitates the testing of JavaScript modules written in the CommonJS format within a Karma test runner environment. Rather than bundling, it applies a lightweight wrapper to individual modules, allowing them to interpret `require()` calls directly in the browser. The current stable version, 1.0.0, was last updated in 2016, making it effectively abandoned, especially given that its primary peer dependency, Karma, has also been officially deprecated since 2024. While `karma-commonjs` boasts potentially faster reloads for individual file changes and provides cleaner stack traces by avoiding full bundling, it requires developers to manually list all module files in the Karma configuration. This contrasts with more modern alternatives like `karma-browserify`, which leverage bundlers to automatically discover and include dependencies while also supporting the full Browserify API for transforms and Node.js shims.","status":"abandoned","version":"1.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/karma-runner/karma-commonjs","tags":["javascript","karma-plugin","commonjs"],"install":[{"cmd":"npm install karma-commonjs","lang":"bash","label":"npm"},{"cmd":"yarn add karma-commonjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add karma-commonjs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core test runner that this plugin extends.","package":"karma","optional":false}],"imports":[{"note":"This package is a Karma plugin and is configured within `karma.conf.js` using string identifiers, not directly imported into user code. 'commonjs' is added to the 'frameworks' array.","wrong":"import { commonjs } from 'karma-commonjs';","symbol":"commonjs","correct":"// karma.conf.js\nmodule.exports = function(config) {\n  config.set({\n    frameworks: ['jasmine', 'commonjs'],\n    // ...\n  });\n};"},{"note":"The 'commonjs' string is used as a preprocessor name in `karma.conf.js` to process files as CommonJS modules.","wrong":"const commonjsPreprocessor = require('karma-commonjs');","symbol":"commonjs preprocessor","correct":"// karma.conf.js\nmodule.exports = function(config) {\n  config.set({\n    // ...\n    preprocessors: {\n      '**/*.js': ['commonjs']\n    }\n  });\n};"},{"note":"Configuration options for the CommonJS preprocessor are nested under `commonjsPreprocessor` in `karma.conf.js`. The `modulesRoot` specifies where `require` calls should look for modules, defaulting to `karma.basePath/node_modules`.","wrong":"config.commonjs.modulesRoot = 'src';","symbol":"commonjsPreprocessor.modulesRoot","correct":"// karma.conf.js\nmodule.exports = function(config) {\n  config.set({\n    // ...\n    commonjsPreprocessor: {\n      modulesRoot: 'src/modules'\n    }\n  });\n};"}],"quickstart":{"code":"const path = require('path');\n\nmodule.exports = function(config) {\n  config.set({\n    basePath: '',\n    frameworks: ['jasmine', 'commonjs'],\n    files: [\n      // Your source files (e.g., 'src/**/*.js')\n      'test/lib.js',\n      'test/**/*.spec.js'\n    ],\n    exclude: [],\n    preprocessors: {\n      'test/lib.js': ['commonjs'],\n      'test/**/*.spec.js': ['commonjs']\n    },\n    reporters: ['progress'],\n    port: 9876,\n    colors: true,\n    logLevel: config.LOG_INFO,\n    autoWatch: true,\n    browsers: ['ChromeHeadless'],\n    singleRun: false,\n    concurrency: Infinity,\n    \n    // Optional configuration for commonjsPreprocessor\n    commonjsPreprocessor: {\n      modulesRoot: path.resolve(__dirname, 'src') // Example: look for required modules in 'src' folder\n    }\n  });\n};\n\n// Example test/lib.js:\n// module.exports = { greet: (name) => `Hello, ${name}!` };\n\n// Example test/lib.spec.js:\n// const { greet } = require('./lib');\n// describe('greet', () => {\n//   it('should return a greeting', () => {\n//     expect(greet('World')).toBe('Hello, World!');\n//   });\n// });","lang":"javascript","description":"This configuration demonstrates how to set up Karma with `karma-commonjs` to test CommonJS modules. It includes the `commonjs` framework and preprocessor, along with an example of configuring `modulesRoot`."},"warnings":[{"fix":"Consider migrating to modern test runners and module bundlers/transpilers (e.g., Jest, Vitest, Web Test Runner with Rollup/Webpack) which offer native ESM/CJS support and better performance.","message":"The underlying Karma test runner is officially deprecated. `karma-commonjs` is not actively maintained and is unlikely to receive updates or security fixes.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure every file that acts as a CommonJS module (source or test) is listed in the `files` array and associated with the `commonjs` preprocessor. Failing to do so will result in `require` not being defined for undeclared modules.","message":"Unlike bundler-based solutions (e.g., `karma-browserify`), `karma-commonjs` requires you to explicitly specify ALL CommonJS module files in the `files` array and `preprocessors` configuration within `karma.conf.js`. It does not automatically discover `require()`d dependencies.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"If your project relies on advanced Browserify features, consider using `karma-browserify` instead. For simple CommonJS module testing without complex bundling needs, `karma-commonjs` might suffice but be aware of its limitations.","message":"`karma-commonjs` provides a simpler CommonJS wrapping mechanism but lacks the full feature set of a bundler like Browserify. It does not natively support Browserify transforms, plugins, or shims for Node.js core modules.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure the file (and its dependencies) are listed in `karma.conf.js` within the `files` array and have the `commonjs` preprocessor applied: `preprocessors: { 'path/to/my-module.js': ['commonjs'] }`.","cause":"A file attempting to use `require()` was not processed by the `commonjs` preprocessor, or a required module was not included in Karma's `files` configuration.","error":"ReferenceError: require is not defined"},{"fix":"Verify the module's path is correct relative to the file requiring it, or adjust the `commonjsPreprocessor.modulesRoot` option in `karma.conf.js` to point to the directory containing your modules.","cause":"The module being `require()`d is not found at the expected path, or the `commonjsPreprocessor.modulesRoot` is incorrectly configured.","error":"Error: Cannot find module 'some-module'"}],"ecosystem":"npm"}