npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

instance-locker

v1.1.5

Published

Locks an application to a singel instance by using a mutex file.

Downloads

9

Readme

Instance Locker

Lightweight and simple instance locking library. It can limit app instances to run as a single instance either in the global or user scope. The locker has synchronized and asynchronous capabilities where the async version is built on top of promises.

Locker

To initiate a locker. Request a new locker object from the instance-locker.

const il = requier("instance-locker");

const locker = il("LockName");

locker
  .Lock()
  .then(success => {
    if (success) {
      console.log("Locker Taken!");
      locker.Unlock().catch(err => console.error(err));
    } else {
      console.log("Locker Occupied!");
    }
  })
  .catch(err => console.error(err));

To use the synchronized version of the locker. Pass a true boolean value as the second parameter when requesting the object. Notice that by design the locker is either an asynchronous locker or synchronized locker.

const il = requier("instance-locker");

const locker = il("LockName", true);

try {
  let success = locker.Lock();

  if (success) {
    locker.Unlock();
    console.log("Locker Taken!");
  } else {
    console.log("Locker Occupied!");
  }
} catch (err) {
  err => console.error(err);
}

Instance-Locker Initialization

The locker init function is quite straight forward. It only requires a lock name to function. But can be configured in slightly different ways.

Locker(lockName[,synchronized[,global[,path]]])

  • lockName string - name of the locker. Should be unique to the application.
  • synchronized boolean - indicates if a async or sync locker should be used. Default: false
  • global boolean - indicates if the lock should be global or user specific. Default: true
  • path string - adds extra uniqueness to the lock name. Default: empty.

Returns a new Locker object that's either synchronized or asynchronous.

Locker.Lock() - Async version

Returns a promise that resolves to a boolean value indicating success or failure to take the locker. If an error occurs the promise rejects with the error.

Locker.Lock() - Sync version

Returns a boolean value indicating success or failure to take the locker. Will throw an error if something significant occurs while trying to grab the locker.

Locker.Unlock() - Async version

Returns a promise that doesn't return a value when resolved. If an error occurs the promise rejects with an error.

Locker.Unlock() - Sync version

Throws an error if something significant occurs while unlocking and removing the locker.

Locker.GetOwnerPID() - Async Version

Returns a promise that eventually resolves to the PID of the lock owner otherwise it resolves to -1 if there is no lock. Promise rejects if an error occures during the process.

Locker.GetOwnerPID() - Sync Version

Returns PID of the owner. If there's no lock -1 will be returned. Errors will be thrown if the method fails.

Side note.

All methods throw file related errors and should be handled by the user.

mechanisms at work

The instance locker follows these steps to ensure that only one instance can take the locker.

  1. Create the lock file. If it doesn't exist jump to step 4.
  2. Check the lock staleness. If stale then jump to step 1.
  3. Notify that the lock is occupied. End here
  4. Write the lock file.
  5. Notify that the lock has been taken successfully.

This should ensure that no race condition occurs if multiple instances of the same application are run back to back. Thus forces all but one instance to fail the locking procedure.

Side notes

The library is not limited to only instance locking. An example is file locking between applications that use this module.

To contribute to the project visit the github page at https://github.com/Idono87/instance-locker

Changelog

  • 1.1.5
    • Fixed a bug that created different lock-file names if the applications were run with different CWDs.
  • 1.1.4
    • Fixed a bug that that caused the lock file to be removed when the process exits.
  • 1.1.3
    • Removed debugging code.
  • 1.1.2
    • Fixed a bug in the async locker where staleness checks infinitely looped when the lock was available.
  • 1.1.1
    • Added Typescript Definition files.
  • 1.1.0
    • Added "GetOwnerPid" method to the async and sync lockers.