Jimple

Lightweight, powerful dependency injection container for Node.js and browsers

~1KB minified Zero dependencies TypeScript support Universal

Features

โšก

Lightweight

~1KB minified and gzipped with zero dependencies

๐ŸŒ

Universal

Works seamlessly in Node.js and browsers

๐Ÿ”ท

TypeScript

Fully typed with excellent IDE support

๐ŸŽฏ

ES6 Proxy

Modern syntax with property access

๐Ÿงช

Well Tested

100% code coverage and stable API

๐Ÿ”ง

Extensible

Easy to extend and customize

Why Dependency Injection?

Dependency injection helps you write more maintainable, testable code by:

  • Decoupling components - Services don't need to know how their dependencies are created
  • Improving testability - Easy to swap dependencies with mocks during testing
  • Managing complexity - Centralized configuration of how objects are wired together
  • Lazy loading - Services are only created when needed
  • Singleton by default - Same instance returned on subsequent calls
  • Dependency management - Services can depend on other services

Quick Start

Get up and running with Jimple in minutes:

Interactive Example
Click "Run" to execute the code

Installation

npm install jimple@2.0.0-beta.3
pnpm add jimple@2.0.0-beta.3
yarn add jimple@2.0.0-beta.3
bun add jimple@2.0.0-beta.3

CDN (Browser)

<script src="https://cdn.jsdelivr.net/npm/jimple@latest/src/Jimple.js"></script>

โš ๏ธ Production Warning: Replace latest with a specific version for production use.

Import Methods

// ES6 Modules
import Jimple from "jimple";

// CommonJS
const Jimple = require("jimple");

// AMD
define(["jimple"], function(Jimple) {
  // Your code here
});

Core Concepts

Services

Services are objects that perform tasks in your application. They're defined as functions that return the service instance:

Services Example
Click "Run" to execute the code

Parameters

Parameters store configuration values, strings, numbers, or any non-function data:

Parameters Example
Click "Run" to execute the code

Factory Services

When you need a new instance every time instead of a singleton:

Factory Services Example
Click "Run" to execute the code

Advanced Features

Protecting Functions

To store an actual function (not a service factory) as a parameter:

container.set('utility', container.protect(() => {
  return Math.random() * 100;
}));

const utilityFn = container.get('utility'); // Returns the function itself
const result = utilityFn(); // Call the function

Extending Services

Add behavior to existing services:

container.set('logger', (c) => new Logger());

// Extend the logger to add file output
container.extend('logger', (logger, c) => {
  logger.addFileHandler('/var/log/app.log');
  return logger;
});

Optional Dependencies & Defaults

Handle optional services with fallbacks:

container.set('cache', (c) => {
  if (c.has('redisConfig')) {
    return new RedisCache(c.get('redisConfig'));
  }
  return new MemoryCache(); // Fallback
});

Raw Service Access

Get the service definition function instead of the service itself:

container.set('database', (c) => new Database());

const dbFactory = container.raw('database');
const db1 = dbFactory(container);
const db2 = dbFactory(container); // Create another instance manually

ES6 Proxy Mode

Use modern JavaScript syntax for a more natural API:

Proxy Mode Example
Click "Run" to execute the code

Limitations:

  • Can't overwrite built-in methods (set, get, etc.)
  • Accessing non-existent properties throws an error
  • TypeScript requires special handling (see below)

TypeScript Support

Jimple provides full TypeScript support with interface definitions:

TypeScript Example
Click "Run" to execute the code

API Reference

Container Methods

MethodDescriptionReturns
set(id, value)Define a service or parametervoid
unset(id, value)Unset a service or parametervoid
get(id)Retrieve a service or parameterany
has(id)Check if service/parameter existsboolean
factory(fn)Create a factory serviceFunction
protect(fn)Protect a function from being treated as serviceFunction
extend(id, fn)Extend an existing servicevoid
raw(id)Get the raw service definitionFunction
register(provider)Register a service providervoid

Need More Details?

For complete API documentation with detailed examples and type definitions:

View Full API Documentation โ†’

More Examples

Express.js Web Server

Express.js Example
Click "Run" to execute the code

Testing with Mocks

Testing Example
Click "Run" to execute the code

Documentation