
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
node-ninject
Advanced tools
nodejs端的DI库,可以方便的在类的构造函数中注入依赖
class TestController {
constructor(testService) {
this.testService = testService; // testService => new TestServie()
}
doAction(args) {
this.testService.doSomething(args);
}
};
npm install node-ninject
or
yarn add node-ninject
config: ninject的映射配置
type: 类型
injects:注入到实例化链中的上下文对象(注入后当前实例化对象和所有依赖关系的实例化对象都拥有该上下文,在nodejs中传递请求上下文很有用)
数组对象,维护类型和别名的映射,ninject从中读取实例化的类型。
{alias: 'testService', bind: TestService}
ninject-config.js
const TestService = require('../services/test-service');
const DepService = require('../services/dep-service');
const Dep2Service = require('../services/dep2-service');
module.exports = [
{alias: 'testService', bind: TestService},
{alias: 'depService', bind: DepService},
{alias: 'dep2Service', bind: Dep2Service},
];
test-controller.js
module.exports = class TestController {
constructor(testService) {
this.testService = testService;
}
doAction(args) {
this.testService.doSomething(args);
}
};
index.js
const ninject = require('node-ninject');
const ninjectConfig = require('./config/ninject-config');
const TestController = require('./controllers/test-controller');
ninject.init(ninjectConfig);
const context = {
req: {
url: 'http://xxx.com'
},
res: {}
};
try {
const testController = ninject.createInstance(TestController, {$context: context});
testController.doAction('I`m is Ninject');
} catch (error) {
console.log(error)
}
node-ninject is licensed under a MIT License.
FAQs
ninject for nodejs
The npm package node-ninject receives a total of 1 weekly downloads. As such, node-ninject popularity was classified as not popular.
We found that node-ninject demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.