{"id":13495538,"url":"https://github.com/mgechev/injection-js","last_synced_at":"2025-05-13T17:07:48.396Z","repository":{"id":37933979,"uuid":"79769025","full_name":"mgechev/injection-js","owner":"mgechev","description":"Dependency injection library for JavaScript and TypeScript in 5.1K. It is an extraction of the Angular's ReflectiveInjector which means that it's well designed, feature complete, fast, reliable and well tested.","archived":false,"fork":false,"pushed_at":"2025-04-22T17:08:32.000Z","size":255,"stargazers_count":1239,"open_issues_count":7,"forks_count":67,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-05-12T23:01:48.791Z","etag":null,"topics":["decorators","dependency-injection","javascript","node","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mgechev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-01-23T04:11:05.000Z","updated_at":"2025-05-12T15:38:31.000Z","dependencies_parsed_at":"2024-11-19T02:09:24.598Z","dependency_job_id":"296dbdbe-2759-41b7-abda-933581f61a67","html_url":"https://github.com/mgechev/injection-js","commit_stats":{"total_commits":54,"total_committers":13,"mean_commits":4.153846153846154,"dds":"0.35185185185185186","last_synced_commit":"2115f2910b55c31091ad1f238b3d6926670ece02"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgechev%2Finjection-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgechev%2Finjection-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgechev%2Finjection-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgechev%2Finjection-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mgechev","download_url":"https://codeload.github.com/mgechev/injection-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990466,"owners_count":21995774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decorators","dependency-injection","javascript","node","typescript"],"created_at":"2024-07-31T19:01:35.686Z","updated_at":"2025-05-13T17:07:47.413Z","avatar_url":"https://github.com/mgechev.png","language":"TypeScript","readme":"![Downloads](https://img.shields.io/npm/dm/injection-js.svg)\n\n# Dependency Injection\n\nDependency injection library for JavaScript and TypeScript in **5.2K**. It is an extraction of the Angular's dependency injection which means that it's feature complete, fast, reliable and well tested.\n\n# Why not Angular version 5 and above?\n\nAngular version 5 deprecated the `ReflectiveInjector` API and introduced `StaticInjector`. In short, the dependency injection in the newest versions of Angular will happen entirely compile-time so reflection will not be necessary.\n\nHowever, if you want to use dependency injection in your Node.js, Vue, React, Vanilla JS, TypeScript, etc. application you won't be able to take advantage of `StaticInjector` the way that Angular will because your application won't be compatible with Angular compiler.\n\nThis means that **if you need dependency injection outside of Angular `@angular/core` is not an option. In such case, use `injection-js` for fast, small, reliable, high-quality, well designed and well tested solution.**\n\n# How to use?\n\n```sh\n$ npm i injection-js\n# OR\n$ yarn add injection-js\n```\n\n\u003e **Note:**\n\u003e\n\u003e For ES5 `Class` syntax and TypeScript you need a polyfill for the [Reflect API](http://www.ecma-international.org/ecma-262/6.0/#sec-reflection).\n\u003e You can use:\n\u003e\n\u003e - [reflection](https://www.npmjs.com/package/@abraham/reflection) (only 3kb 🔥)\n\u003e - [reflect-metadata](https://www.npmjs.com/package/reflect-metadata)\n\u003e - [core-js (`core-js/es7/reflect`)](https://www.npmjs.com/package/core-js)\n\u003e\n\u003e Also for TypeScript you will need to enable `experimentalDecorators` and `emitDecoratorMetadata` flags within your `tsconfig.json`\n\n## TypeScript\n\nUsing the latest `inject` function:\n\n```ts\nimport { inject, ReflectiveInjector } from 'injection-js';\n\nclass Http {}\n\nclass Service {\n  http = inject(Http);\n}\n\nconst injector = ReflectiveInjector.resolveAndCreate([Http, Service]);\n\nconsole.log(injector.get(Service) instanceof Service);\nconsole.log(injector.get(Service).http instanceof Http);\n```\n\nUsing `@Injectable` and `reflect-metadata`:\n\n```ts\nimport 'reflect-metadata';\nimport { ReflectiveInjector, Injectable, Injector } from 'injection-js';\n\nclass Http {}\n\n@Injectable()\nclass Service {\n  constructor(private http: Http) {}\n}\n\n@Injectable()\nclass Service2 {\n  constructor(private injector: Injector) {}\n\n  getService(): void {\n    console.log(this.injector.get(Service) instanceof Service);\n  }\n\n  createChildInjector(): void {\n    const childInjector = ReflectiveInjector.resolveAndCreate([Service], this.injector);\n  }\n}\n\nconst injector = ReflectiveInjector.resolveAndCreate([Service, Http]);\n\nconsole.log(injector.get(Service) instanceof Service);\n```\n\n## ES6\n\n```js\nconst { Inject, ReflectiveInjector } = require('injection-js');\n\nclass Http {}\n\nclass Service {\n  static get parameters() {\n    return [new Inject(Http)];\n  }\n\n  constructor(http) {\n    this.http = http;\n  }\n}\n\nconst injector = ReflectiveInjector.resolveAndCreate([Http, Service]);\n\nconsole.log(injector.get(Service) instanceof Service);\n```\n\n## ES5\n\n```js\nrequire('reflect-metadata');\nvar di = require('injection-js');\n\nvar Http = di.Class({\n  constructor: function() {},\n});\n\nvar Service = di.Class({\n  constructor: [\n    Http,\n    function(http) {\n      this.http = http;\n    },\n  ],\n});\n\nvar injector = di.ReflectiveInjector.resolveAndCreate([Http, Service]);\n\nconsole.log(injector.get(Service) instanceof Service);\n```\n\n# API\n\nFor full documentation check Angular DI docs:\n\n- [Dependency Injection](https://v4.angular.io/guide/dependency-injection)\n- [Dependency Injection in action](https://v4.angular.io/guide/dependency-injection-in-action)\n- [Dependency Injection without Typescript](https://v2.angular.io/docs/ts/latest/cookbook/ts-to-js.html#!#dependency-injection)\n\n# Ecosystem\n\nThis is a list of libraries that are using injection-js. If you have a suggestion on what to add, please don't hesitate to submit a PR.\n\n## Libraries\n\n- [ng-packagr](https://github.com/ng-packagr/ng-packagr) Transpile your libraries to Angular Package Format. Part of the official Angular CLI.\n- [@martin_hotell/axios-http](https://github.com/Hotell/axios-http) Injectable axios HttpClient wrapper for browser and node\n- [@martin_hotell/rea-di](https://github.com/Hotell/rea-di) Dependency injection for React done right. Hierarchical injection on both component and service layer powered by injection-js (Angular DI framework) 🖖\n- [rxstack](https://github.com/rxstack/rxstack) RxStack is a realtime object-oriented framework which helps you build a micro service web applications on top of other frameworks like express and socketio by adding an abstraction layer.\n- [ServeRX-ts](https://github.com/mflorence99/serverx-ts) Experimental [Node.js](https://nodejs.org) HTTP framework using [RxJS](https://rxjs.dev), built with [TypeScript](https://www.typescriptlang.org/) and optimized for serverless deployments. Features declarative routes and dependency injection powered by injection-js.\n\n# License\n\nMIT\n","funding_links":[],"categories":["Repository","TypeScript","Angular-Inspired Solutions"],"sub_categories":["Inversion of control / Dependency Injection (Ioc/DI)","Wrappers"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgechev%2Finjection-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmgechev%2Finjection-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgechev%2Finjection-js/lists"}