single-instance-lock

Module creating single instance lock

Usage no npm install needed!

<script type="module">
  import singleInstanceLock from 'https://cdn.skypack.dev/single-instance-lock';
</script>

README

Single Instance Lock

Single Instance Lock is a module enabling Node.js script running as single instance apps.

Installation

Use following command.

yarn add single-instance-lock

API

Creating simple locker

import { SingleInstanceLock, LockType } from 'single-instance-lock';

const locker = new SingleInstanceLock('some-id');

// creates a single instance lock
locker.lock(LockType.Last);

locker.on('locked', () => {
    // running as single instance
    // your code goes here
});

locker.on('error', (err) => {
    // app with this id is already running
});

locker.unlock(() => {
    // lock was removed        
});

Lock Types


    // ...
    // Keeps lock on the last executed instance and closes the previously executed ones
    locker.lock(LockType.Last);
    // ...

    // ...
    // Keeps lock on the first executed instance and closes the later executed ones
    locker.lock(LockType.First);
    // ...

License

MIT