navigator.mediaDevices is undefined, so getUserMedia cannot be called
TypeError when calling navigator.mediaDevices.getUserMedia on an insecure pageShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
window.isSecureContext is false and navigator.mediaDevices prints undefinedThe page is served over plain HTTP from a non-local addressFix - 2
navigator.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
Works 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
- 1
What does the error look like?
TypeError when calling navigator.mediaDevices.getUserMedia on an insecure pagenavigator.mediaDevices is undefinedNotAllowedError (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
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
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
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
Airflow SSHOperator stops a long script with SSH command timed out
airflow.exceptions.AirflowException: SSH command timed out
SSHOperator waits only 10 seconds for a command by default.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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