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.5
pnpm add jimple@2.0.0-beta.5
yarn add jimple@2.0.0-beta.5
bun add jimple@2.0.0-beta.5

CDN (Browser)

<script src="https://cdn.jsdelivr.net/npm/jimple@2.0.0-beta.5/dist/Jimple.umd.js"></script>

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:

Protect Functions Example
Click "Run" to execute the code

Extending Services

Add behavior to existing services:

Extend Services Example
Click "Run" to execute the code

Removing Services or Parameters

Remove services or parameters from the container with unset():

Removing Services or Parameters Example
Click "Run" to execute the code

Important Notes:

  • Removes the service/parameter completely from the container
  • Clears any cached instances and metadata for services
  • Cannot be undone - you'll need to re-register the service
  • Safe to call on non-existent services (no error thrown)

Optional Dependencies & Defaults

Handle optional services with fallbacks:

Optional Dependencies and Defaults Example
Click "Run" to execute the code

Raw Service Access

Get the service definition function instead of the service itself:

Raw Service Access Example
Click "Run" to execute the code

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

Modular Configuration with Providers

Organize your container configuration into reusable modules. Providers help you structure your application by grouping related services and configuration into logical units.

Basic Provider

A provider is an object with a register method that defines services and parameters:

Basic Provider Example
Click "Run" to execute the code

Multiple Providers

You can register multiple providers to organize different aspects of your application:

Multiple Providers Example
Click "Run" to execute the code

Provider Dependencies

Providers can depend on services defined by other providers, allowing for modular composition:

Provider Dependencies Example
Click "Run" to execute the code

Benefits of Using Providers:

  • Modularity - Group related services together
  • Reusability - Share providers across different applications
  • Organization - Separate concerns into logical modules
  • Testing - Easier to mock entire provider modules

API Reference

Container Methods

MethodDescriptionReturns
set(id, value)Define a service or parametervoid
unset(id, value)Remove 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