Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

navigator.mediaDevices is undefined, so getUserMedia cannot be called

ErrorTypeError when calling navigator.mediaDevices.getUserMedia on an insecure page

Short answer

Browsers only expose navigator.mediaDevices on secure pages: HTTPS, localhost or file://. On a plain http:// address on a real host it is undefined, and calling getUserMedia throws a TypeError. If the property exists but the call still fails, the page sits in an iframe without permission or has no origin.

  • Applies to Chrome 140+, Firefox 140+, Safari 18+
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run this in the console of the exact page that fails. It tells you whether the context is secure and whether the API is there.

    console.log(location.href, window.isSecureContext, navigator.mediaDevices);
  2. 2Match what you see, then jump to its fix

    1. 1window.isSecureContext is false and navigator.mediaDevices prints undefinedThe page is served over plain HTTP from a non-local addressFix
    2. 2navigator.mediaDevices exists, but getUserMedia rejects with NotAllowedError and no prompt appearsThe page runs inside an iframe that was not given camera or microphone accessFix
    3. 3Works when the same code is opened from a normal URL, fails from srcdoc, data: or blob: content or a sandboxed iframeThe document has no origin or sits in a sandboxed frameFix

What does the error look like?

  • TypeError when calling navigator.mediaDevices.getUserMedia on an insecure page
  • navigator.mediaDevices is undefined
  • NotAllowedError (getUserMedia inside an iframe)

Cause 1 of 3

The page is served over plain HTTP from a non-local address

1Check

Look at the address bar. A page opened as http://192.168.x.x:3000 or http://my-server.example is not a secure context; http://localhost is.

console.log(window.isSecureContext, location.protocol, location.hostname);

2Fix

Serve the page over HTTPS (a certificate on the real host, or a local HTTPS dev server for testing on a phone). Opening it as localhost also counts as secure.

// guard so the failure explains itself
if (!navigator.mediaDevices?.getUserMedia) {
  throw new Error("Camera needs HTTPS, localhost or file://");
}

3Verify

After the move to HTTPS, isSecureContext is true and the object exists.

console.log(window.isSecureContext, typeof navigator.mediaDevices);

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

The page runs inside an iframe that was not given camera or microphone access

1Check

Find the embedding iframe tag. The browser asks the user only if the top-level page grants the feature to the frame.

document.querySelectorAll('iframe').forEach(f => console.log(f.src, f.allow));

2Fix

Add the allow attribute on the iframe, and if the server sends a Permissions-Policy header, list the feature there too.

<iframe src="https://app.example.com/call" allow="camera; microphone"></iframe>

3Verify

Reload the parent page. The permission prompt now appears for the framed app.

navigator.mediaDevices.getUserMedia({ video: true }).then(s => console.log('ok', s.id));

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

The document has no origin or sits in a sandboxed frame

1Check

Inspect how the page was loaded: a srcdoc attribute, a data: URL, or a sandbox attribute on the iframe.

console.log(location.origin, window.origin);

2Fix

Load the page from a real HTTPS URL. If the frame has a sandbox attribute, include allow-same-origin.

<iframe src="https://app.example.com/call"
        sandbox="allow-scripts allow-same-origin"
        allow="camera; microphone"></iframe>

3Verify

location.origin shows a real origin, not the string null, and getUserMedia prompts.

console.log(location.origin);

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Develop with https or localhost from day one, so the camera code never meets a plain-HTTP address.
  • Guard every call with navigator.mediaDevices?.getUserMedia and show a clear message when it is missing.
  • Document the iframe allow attribute next to the embed snippet you hand to partners.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.