Decorator based service / dependency injection for NodeJS and Typescript inspired from Angular and Spring.
Node Injection allows you to develop services that can be share throughout your application. Each service has a single instance which can be injected into other services, controllers, classes etc.
Here is a basic example of how to use Node Injection:
// 1. Import ES6 polyfills;;// 2. Create a service// 3. Inject your servicenew A; // prints "1" to the consolenew B; // prints "2" to the console
Services do not need to use
@Inject(): you just declare in their constructor the services to inject, and Node Injection injects them automatically:
- Install module:
npm install node-injection --save
- If you are targeting ES5, install
and make sure to import it in the global scope (place it at the beginning of your application):
npm install reflect-metadata es6-shim --save;;
- In your
tsconfig.json, make sure you have the following options:
"emitDecoratorMetadata": true, "experimentalDecorators": true,
Node Injection annotations are built with simplicity, clearness and conciseness in mind. The full list of features they allow is described below.
This class decorator registers a class as a service.
Node Injection creates a single instance of this class. When another class or service injects this service, they access the same instance.
Services can rely on other services, all you need to do is declare dependencies in their constructor and they will automatically be injected with the appropriate instance at runtime.
You will get an error if you try to inject a service that is not registered (i.e. is not annotated with
Also note that services are identified by their class name. Hence, you cannot register two classes with the same name as services.
@Service() decorator can be given a configuration object to alter its behavior.
|name||String||Override the name used to register the service. See usage with uglified code.||
|injectable||Boolean||When set to
Usage with uglified code
By default, Node Injection uses
Function.prototype.name to determine the name of the service to register (unless one is specified), and also the type of service to inject. When using this library in uglyfied code (Webpack.optimize.UglifyJsPlugin, UglifyJS...), function names are generally mangled (replaced by shorter names). This will cause service registration conflicts and injection errors.
There are two ways to prevent this problem:
Provide a name for each of your service:
When defined, Node Injection will use this property instead of
Disable function name mangling. For Webpack's UglifyJS plugin, this is how you do it:sourceMap: true mangle: keep_fnames: true
The disadvantage of this approach is that code obfuscation will be reduced.
Constructor is used to emphasize that the given parameter must be a class. It is defined as:
which simply represents a class object:
MyClasswould be the argument.
Sometimes, you want to inject services in classes that aren't services themselves. You can use the
@Inject() property decorator to this end.
Working with interfaces
When a property type is an interface, you need to tell Node Injection which class to inject:
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
- Geoffroy Empain - Initial work
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details.