{"id":17412,"library":"yarb","title":"yarb JavaScript Bundler","description":"yarb (Yet Another Require Bundler) is a JavaScript module bundler designed for the browser environment, serving a similar purpose to Browserify. It focuses on bundling CommonJS-style modules and is currently at version 0.8.0. While its release cadence isn't explicitly stated, the version number suggests a more experimental or niche tool compared to its established alternatives. A key differentiator of yarb is its enhanced capability for defining dependencies between bundles, automatically identifying and excluding common files when using the `external` function, which simplifies multi-bundle setups. However, yarb is less flexible than Browserify, offering limited support for Node.js core modules (only `events`, `fs`, `module`, `net`, `path`, `stream`, `util`) and fewer configuration options. Internally, it utilizes vinyl objects for file representation, allowing for flexible input of buffers or streams, provided they have unique and absolute `path` properties.","status":"maintenance","version":"0.8.0","language":"javascript","source_language":"en","source_url":"https://github.com/mwiencek/yarb","tags":["javascript","require","bundle","browser"],"install":[{"cmd":"npm install yarb","lang":"bash","label":"npm"},{"cmd":"yarn add yarb","lang":"bash","label":"yarn"},{"cmd":"pnpm add yarb","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"yarb is a CommonJS-first library. Direct ES module imports are not supported without a transpilation or bundling step.","wrong":"import yarb from 'yarb'","symbol":"yarb","correct":"const yarb = require('yarb')"},{"note":"The library exports the bundler function as its module.exports directly. There are no named exports like `yarb.yarb`.","wrong":"const yarb = require('yarb').yarb","symbol":"yarb","correct":"const bundlerInstance = yarb(['./src/entry.js'])"},{"note":"The `yarb` function itself returns a new bundle instance. There is no explicit `Bundle` class constructor exposed directly to the user.","wrong":"const bundle = new yarb.Bundle(['./entry.js'])","symbol":"BundleInstance","correct":"const bundle = yarb(['./entry.js'])"}],"quickstart":{"code":"const yarb = require('yarb');\nconst path = require('path');\nconst fs = require('fs');\n\n// Define a simple custom Browserify-compatible transform\nfunction myTransform(file) {\n  let data = '';\n  const stream = require('stream');\n  const tr = new stream.Transform();\n  tr._transform = function (chunk, encoding, callback) {\n    data += chunk.toString();\n    callback();\n  };\n  tr._flush = function (callback) {\n    this.push(Buffer.from(data.replace(/foo/g, 'bar')));\n    callback();\n  };\n  return tr;\n}\n\nconst mainDirPath = path.join(__dirname, 'yarb-src');\nconst mainFilePath = path.join(mainDirPath, 'main.js');\nconst utilFilePath = path.join(mainDirPath, 'utils', 'helper.js');\nconst outputFilePath = path.join(__dirname, 'yarb-bundle.js');\n\n// Create dummy files for demonstration\nfs.mkdirSync(path.dirname(utilFilePath), { recursive: true });\nfs.writeFileSync(mainFilePath, \"console.log(require('./utils/helper').greet('World') + ' foo');\");\nfs.writeFileSync(utilFilePath, \"exports.greet = (name) => `Hello, ${name}!`;\");\n\nconst bundle = yarb([mainFilePath], { debug: true }) // Enable source maps\n  .add(utilFilePath) // Add utility file explicitly if not directly required\n  .transform(myTransform); // Apply the custom transform\n\nbundle.bundle((err, buf) => {\n  if (err) {\n    console.error('Bundling failed:', err);\n    return;\n  }\n  console.log('Bundle created successfully. Writing to', outputFilePath);\n  fs.writeFileSync(outputFilePath, buf.toString());\n  console.log('--- Bundled Output (truncated) ---\\n' + buf.toString().substring(0, 500));\n\n  // Clean up dummy files\n  fs.unlinkSync(mainFilePath);\n  fs.unlinkSync(utilFilePath);\n  fs.rmdirSync(path.dirname(utilFilePath));\n  fs.rmdirSync(mainDirPath);\n  console.log('\\nDummy files cleaned up.');\n});","lang":"javascript","description":"Demonstrates basic usage of yarb to bundle a main entry file with a utility, apply a custom Browserify-compatible transform, and output the result to a file."},"warnings":[{"fix":"Ensure your browser bundle does not rely on unsupported Node.js core modules. Consider using browser-compatible alternatives or polyfills if absolutely necessary.","message":"yarb only supports a limited set of Node.js core modules: `events`, `fs`, `module`, `net`, `path`, `stream`, `util`. Attempting to require other core modules (e.g., `http`, `crypto`) will result in runtime errors in the browser.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Before passing Vinyl objects to `yarb` (or `add`, `require`, `expose`), ensure their `path` properties are absolute and globally unique within the bundle context. Use `path.resolve()` if necessary.","message":"When providing Vinyl objects as input, each object MUST have a unique and absolute `path` property. These paths are crucial for module resolution and must not conflict or be relative.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"If a transform needs to be applied globally (including `node_modules`), pass a `{ global: true }` option to the `transform` method: `bundle.transform(myTransform, { global: true })`.","message":"Transforms, by default, are not applied to code located within `node_modules/` directories. This behavior is designed to prevent unintended modifications to third-party dependencies.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Carefully manage the loading order of your bundles in the browser. External bundles should always be `<script>`-loaded before dependent bundles to ensure proper resolution.","message":"When using `bundle.external(externalBundle)`, the `externalBundle` must be loaded on the page *before* any other bundles that reference modules within it. Incorrect loading order will lead to runtime 'module not found' errors.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Evaluate if yarb meets your specific bundling needs. For complex scenarios, extensive core module polyfills, or a wider range of configuration options, consider Browserify or webpack.","message":"yarb is significantly less flexible and feature-rich than Browserify. It lacks many of Browserify's advanced settings, behaviors, and comprehensive core module shims, making it suitable for simpler bundling tasks.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure the bundled output from `bundle.bundle()` is properly included in your HTML, typically within a `<script>` tag. Verify the bundle generation process completed successfully and produced output.","cause":"Attempting to run a yarb-bundled file in a browser without the necessary CommonJS runtime preamble, or the bundle itself is empty.","error":"Uncaught ReferenceError: require is not defined"},{"fix":"Check the module path for correctness. If it's not referenced from an entry point, use `bundle.add('./path/to/my-module-name.js')` or `bundle.require('./path/to/my-module-name.js')` to explicitly include it.","cause":"A `require()` call refers to a module that yarb could not locate or include in the bundle. This can happen if the file is not an entry point, not explicitly added via `bundle.add()` or `bundle.require()`, or if the path is incorrect.","error":"Error: Cannot find module 'my-module-name'"},{"fix":"Remove dependencies on unsupported Node.js core modules. For browser-side functionality, find alternative libraries that do not rely on Node.js specifics.","cause":"Attempting to require a Node.js core module that yarb does not support or shim for the browser environment.","error":"Error: Cannot find module 'http'"},{"fix":"Inspect previous steps in the bundling chain for errors or misconfigurations. Ensure entry files exist and are correctly specified. Always handle the stream or provide a callback to `bundle.bundle()`.","cause":"This typically occurs when `bundle.bundle()` returns `null` or `undefined`, indicating a configuration error or an unhandled internal error before the stream is created, or when not using a callback.","error":"TypeError: Cannot read properties of undefined (reading 'pipe')"}],"ecosystem":"npm","meta_description":null}