Skip to content

Installation

Terminal window
npm install use-media-stream
Terminal window
# or
yarn add use-media-stream
pnpm add use-media-stream
bun add use-media-stream

That’s it. The package has zero runtime dependenciesreact >= 16 as a peer dependency is the only thing it expects, and you already have that.

The package ships both module formats with types for each, so it works whether your project is ESM, CommonJS, or bundled:

import useMediaStream from 'use-media-stream'; // ESM
const useMediaStream = require('use-media-stream'); // CommonJS

It is published with provenance, so you can verify the tarball on npm was built by the repository’s release workflow:

Terminal window
npm audit signatures
React >= 16 (peer dependency)
Node >= 18, only if you render server-side
Browser Anything with getUserMedia — every current browser

getUserMedia requires a secure context, so the camera works on https:// and on localhost, but not on a plain http:// origin.

v2 is a correctness release. The API is unchanged apart from additions, but the lifecycle fixes change behaviour by definition — two of them turn the camera off in cases where v1 left it on.

See Migrating from v1 for the full list. The short version:

  • stop() releases any stream the hook holds, not just one started by start()
  • Unmounting releases the stream
  • error is Error | null, was unknown
  • Request states are a union, were string
  • Quick start — a working camera in about fifteen lines
  • Live demo — every function wired to a real camera