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

detect-primary-camera

v1.0.1

Published

Zero-dependency library to robustly detect the primary rear camera on mobile devices

Readme

GitHub Actions Workflow Status NPM Version NPM Downloads Socket Badge License: MIT

💡 The Motivation

Dual-camera, triple-camera, and quad-camera phones expose multiple lenses (ultra-wide, telephoto, macro, infrared) to the browser. This often leads to web applications selecting the wrong or suboptimal camera.

The Proctored Exam Problem: I went through this exact issue while appearing for a certification exam. The exam's web app selected my phone's macro lens, making my ID and face blurry and unrecognizable. This frustrating experience led me to build a better, reusable, and fully open-source utility to solve this problem once and for all.

Whether you're building a barcode scanner, a proctored exam interface, or just a standard photo capture app, this library safely filters and returns the main/wide camera that is best suited for the job. 🎯

✨ Features

  • Modern Device Support: Explicitly penalizes ultra-wide, telephoto, and macro lenses which are terrible for general purposes like barcode scanning or exam proctoring.
  • Cross-Platform: Handles iOS ("Back Camera") and Android ("camera 0") quirks seamlessly.
  • Framework Agnostic: Works perfectly in Vanilla JS/TS, with dedicated hooks available for React and Next.js.
  • Caching: Utility to persist the optimal device ID, speeding up subsequent application loads.
  • Zero Dependencies: Tiny bundle size to keep your application fast.

📦 Installation

npm install detect-primary-camera

🚀 Usage

Standard JavaScript / TypeScript

import { getBestRearCamera, getAndSetBestRearCamera, FACING_MODE } from 'detect-primary-camera';

// Example 1: Get the best camera directly
const cameraId = await getBestRearCamera();
if (cameraId) {
  const stream = await navigator.mediaDevices.getUserMedia({
    video: {
      deviceId: { exact: cameraId }
    }
  });
}

// Example 2: Detect the best camera and cache the result for the next time
const cachedCameraId = await getAndSetBestRearCamera();

React / Next.js

For React applications, a dedicated hook is provided to handle resolution state and cleanup automatically.

import { usePrimaryCamera } from 'detect-primary-camera/react';

const CameraComponent = () => {
    // Automatically uses cached IDs and handles loading/error states cleanly!
    const { cameraId, loading, error } = usePrimaryCamera();

    if (loading) return <div>Detecting camera...</div>;
    if (error) return <div>Error: {error.message}</div>;

    return <video autoPlay playsInline muted id={cameraId} />;
};

🤝 Contributing

We welcome contributions from the community! Whether it's adding a new feature, fixing a bug, or improving documentation, your help is greatly appreciated.

Please see the CONTRIBUTING.md file for more information on how to get started, set up your development environment, and submit a Pull Request.

📄 License

MIT © Sumit Sahoo

Please refer to the LICENSE file for the complete license details.