Fix navigator.clipboard is undefined
If you use the modern Clipboard API in your JavaScript code then you may already see the following error message during development in the browser's console:
Uncaught TypeError: Cannot read properties of undefined (reading 'writeText')
This is because:
This feature is available only in secure contexts (HTTPS) or with localhost.
Solution
As the Clipboard API is the modern way of accessing the clipboard instead of the old solution that used the document.execCommand()
so don't change your code. Instead try one of the following:
- Configure HTTPS for your local development URL.
- Temporarily disable the browser's security check for the test domain. To do this in Chrome visit
chrome://flags/
and enable the Insecure origins treated as secure option and add the local domain as an exception.
Reference