Install.js CommonJS Module Loader for Browsers

0.13.0 · abandoned · verified Sun Apr 19

Install.js is a minimal JavaScript module loader designed primarily to deliver CommonJS modules to web browsers, addressing the challenge of synchronous `require` semantics in a browser environment. While it provides a feature-rich, small-footprint solution for its original purpose, its development has been inactive since its last update in 2018 (version 0.13.0). The package offers explicit control over module installation and resolution, including options for file extensions, package.json `mainFields`, and a `fallback` mechanism for unresolved modules. It was created in an era before modern bundlers (like Webpack, Rollup) and native ECMAScript Modules (ESM) became standard practice, making its direct use in new projects less common today.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `install` module loader, define and install custom CommonJS modules, and then `require` them using the loader's context. It shows the three main steps: `makeInstaller`, `install`, and `require`.

const makeInstaller = require('install').makeInstaller;

// Step 1: Create an install function with optional configurations
const install = makeInstaller({
  extensions: [".js", ".json"],
  // fallback: (id) => { /* custom resolution logic, e.g., native Node.js require */ },
  browser: typeof window !== 'undefined',
  mainFields: ["browser", "main"]
});

// Step 2: Install modules using a nested object tree
const localRequire = install({
  "main.js"(require, exports, module) {
    const assert = require("assert"); // Example: requiring a module within the installed context

    assert.strictEqual(
      require("package").name,
      "/node_modules/package/entry.js"
    );

    exports.name = module.id;
  },

  node_modules: {
    package: {
      "package.json"(require, exports, module) {
        exports.name = "package";
        exports.version = "0.1.0";
        exports.main = "entry.js";
      },
      "entry.js"(require, exports, module) {
        exports.name = module.id;
      }
    }
  }
});

// Step 3: Require the entry point module(s) to evaluate them
console.log(localRequire("./main").name);
// Expected output: "/main.js"

view raw JSON →