Service Provider Tutorial

This tutorial will explain how to create and use a service provider.

A Service Provider registers services that provide features for OS.js.

Service Providers have the same interface and initialization/startup procedure in both client and server.


To create an instance of a service provided:

core.make('service-name', ...args);

Custom Service Provider

This is the service provider interface:

class ServiceProvider
  constructor(core, options = {}) {
    this.core = core;
    this.options = options;

  /* The list of registered services */
  provides() {
    return [];

  /* An optional list of dependant services */
  depends() {
    return [];

  /* Initialize your services */
  async init() {}

  /* Start your services. Runs after all services has been init-ed */
  start() {}

  /* Clean up */
  destroy() {}

See service provider guide for more info.

To generate a new provider using the example via CLI run npm run make:provider.

For general information about development see development article.

Instance factory

To register a factory:

core.instance('service-name', (...args) => new SomeClass(...args));

Singleton factory

To register a singleton:

core.singleton('service-name', () => new SomeClass({foo: 'bar'}));

Basic example

Register a singleton in form of an object with a method that opens alert():

// src/client/myprovider.js

import {ServiceProvider} from '@osjs/common'; // Optional: Use the official base class

export class MyApiServiceProvider extends ServiceProvider
  provides() {
    return ['namespace/api'];

  async init() {
    this.core.singleton('namespace/api', () => ({
      greet: name => alert(`Hello ${name}!`)

// src/client/index.js
import {MyApiServiceProvider} from './myprovider.js';

// ...
// ...

You can test this by entering the following in your browser developer console:


and you should see a browser alert box.

OS.js Web Desktop - © Anders Evenrud <>

results matching ""

    No results matching ""