CommonJS Everywhere Bundler

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

commonjs-everywhere (cjsify) is a browser bundler designed for CommonJS modules, providing features like source map generation from minified bundles back to original source code, module aliasing for browser-specific overrides, and an extensible architecture to support various compile-to-JavaScript languages. Its current stable version is 0.9.7, and it appears to be unmaintained given its reliance on Node.js 0.8.x, 0.9.x, or 0.10.x (versions that were released over a decade ago and are long End-of-Life). Key differentiators in its time included robust source map support from minified to original sources, the ability to define custom handlers for non-JavaScript files (such as CoffeeScript and JSON, which were included by default), and an efficient watch mode that only rebuilt changed dependencies. It served as an early alternative to tools like Browserify for enabling Node.js-style module loading in browser environments.

error Error: Cannot find module 'commonjs-everywhere'
cause The package is not installed or the `require()` path is incorrect.
fix
Ensure commonjs-everywhere is installed locally (npm install commonjs-everywhere) or globally (npm install -g commonjs-everywhere). Check the require() path for accuracy.
error Error: 'require' not defined
cause Attempting to use CommonJS `require()` in an environment that does not define it (e.g., a browser without a bundler's shim), or using `import` syntax where `require()` is expected.
fix
Ensure you are running the code in a Node.js environment or that the bundled output includes the necessary CommonJS shim. If using the programmatic interface, ensure you are calling cjsify and then processing its output. If you tried to use import, switch to const cjsify = require('commonjs-everywhere');.
error SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES Modules `import` syntax with this CommonJS-only package, or in a Node.js environment not configured for ESM.
fix
This package is strictly CommonJS. Use const cjsify = require('commonjs-everywhere'); instead of import.
breaking The package is explicitly compatible only with very old Node.js versions (0.8.x, 0.9.x, 0.10.x). It is highly unlikely to run on modern Node.js versions without significant issues or failure.
fix Use a compatible Node.js version (e.g., via `nvm`) if absolutely necessary, but migration to a modern bundler is strongly recommended.
abandoned commonjs-everywhere is no longer actively maintained. The last release was several years ago, and its dependencies and underlying technologies are outdated.
fix Migrate to a contemporary JavaScript bundler such as Webpack, Rollup, Parcel, or esbuild for new projects and consider migrating existing projects away from this tool.
gotcha When using the programmatic interface (`cjsify(entryPoint, root, options)`), the function returns a SpiderMonkey AST (Abstract Syntax Tree), not executable JavaScript code. You must use a separate tool like `escodegen` to convert the AST into a JavaScript string.
fix Install `escodegen` (`npm install escodegen`) and use it to generate code: `const bundledCode = escodegen.generate(ast);`
gotcha Using unmaintained software, especially build tools, can introduce security vulnerabilities through unpatched dependencies, deprecated APIs, or inherent flaws that will not receive updates.
fix Avoid using this package for new development. For existing projects, perform a thorough security audit or, preferably, migrate to a actively maintained and secure bundling solution.
npm install commonjs-everywhere
yarn add commonjs-everywhere
pnpm add commonjs-everywhere

Demonstrates bundling a simple CommonJS application using the `cjsify` CLI, exporting a global variable, and generating a source map.

# Create dummy files for demonstration
mkdir -p cjs-app/src
echo 'module.exports = "Hello from dep!";' > cjs-app/src/dep.js
echo 'const dep = require("./dep"); module.exports = () => `Main output: ${dep}`;' > cjs-app/src/entry-file.js

# Navigate into the app directory
cd cjs-app

# Bundle the entry file, export 'MyLibrary', and create a source map
# This will output the bundled code to stdout
npx commonjs-everywhere src/entry-file.js --export MyLibrary --source-map my-library.js.map >my-library.js

# To see the output
cat my-library.js
cat my-library.js.map

# Clean up (optional)
cd ..
rm -rf cjs-app