JS Bundler
js-bundler is a command-line tool designed for bundling JavaScript and CoffeeScript files, along with their 'required' dependencies, into a single output file. Currently at version 1.2.4, it was last published over 7 years ago, indicating an abandoned status. It differentiates itself from more feature-rich bundlers like Browserify by explicitly *not* providing Node.js built-in module shims or source mapping, aiming for a lightweight profile. This makes it suitable primarily for client-side browser or 'neutral' packages that do not rely on Node.js-specific modules. Its release cadence is effectively non-existent, given its age. Modern alternatives like Vite, Rollup, esbuild, and Parcel offer significantly faster build times, better developer experience, and more comprehensive feature sets, including native ES module support and advanced optimizations like tree-shaking, which js-bundler lacks.
Common errors
-
Error: A file matching a negation pattern was required: <path/to/file.js>
cause A file or directory that matches a pattern specified with the `-n` option was attempted to be included in the bundle.fixAdjust your negation patterns (`-n`) or refactor your code to avoid requiring files that should be excluded. The process will exit with code 64 upon this error. -
ReferenceError: require is not defined
cause Attempting to bundle Node.js-specific modules (e.g., using `require('fs')`) for a browser environment where Node.js built-in shims are not provided.fixEnsure your project only requires 'browser-safe' modules, or manually shim/mock Node.js built-ins if absolutely necessary (though this tool is designed *without* such shims).
Warnings
- gotcha js-bundler explicitly does not provide shims for Node.js built-in modules (e.g., `fs`, `path`). Bundling code that relies on these modules will result in runtime errors in environments where these shims are not present (e.g., browsers).
- gotcha The bundler does not generate source maps, making debugging bundled code significantly more challenging as original file locations are not preserved.
- security Using the `-i` option (informative data) embeds raw file paths into the bundled output. This can disclose sensitive information about your project's directory structure, especially in downloadable versions.
- gotcha The `-n` (negate pattern) option prevents files or directories matching a given pattern from being bundled. If a required file matches a negation pattern, the bundler will exit with code 64.
- gotcha When using the `module.` prefix trick (e.g., `var abc = module.require('./abc');`) for server-side code, `module.require` will throw an error if the surrounding `if` condition fails to prevent its execution in an unsupported environment (e.g., browser).
Install
-
npm install js-bundler -
yarn add js-bundler -
pnpm add js-bundler
Imports
- bundle (CLI)
require('js-bundler')bundle <file> [options]
- bundle -c (CoffeeScript)
bundle example.coffee
bundle -c:coffee 'coffee -bcs' example.coffee
- bundle -d (Dummy Modules)
bundle example.js
bundle -d 'module-to-dummy' example.js
Quickstart
#!/bin/bash
# Ensure js-bundler is installed globally or linked
# npm install -g js-bundler
# Create a simple JavaScript file that requires another module
cat <<EOF > main.js
const dep = require('./dependency.js');
console.log('Main script running, dependency says: ' + dep.message);
EOF
cat <<EOF > dependency.js
exports.message = 'Hello from dependency!';
EOF
echo "\n--- Bundling main.js to stdout ---"
bundle main.js
echo "\n--- Bundling main.js to bundle.js ---"
bundle main.js > bundle.js
echo "\n--- Content of bundle.js ---"
cat bundle.js