You're not loading the key from the server side, because it's running in the client. The key needs to be present in the client-side JavaScript bundle. The easiest way to include it is by configuring the replace
plugin (if you're using Rollup) or the DefinePlugin
(in webpack).
In Rollup, update this block in the config:
replace({
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode),
'process.env.RECAPTCHA_KEY': my_recaptcha_key
})
In webpack, update this block:
new webpack.DefinePlugin({
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode),
'process.env.RECAPTCHA_KEY': JSON.stringify(my_recaptcha_key)
})
Then, you can refer to that value in your code:
script.src = `https://www.google.com/recaptcha/api.js?render=${process.env.RECAPTCHA_KEY}`;
In future, please avoid using screenshots to provide code samples — use markdown instead. It makes pages more searchable, more accessible to people using assistive technology, and means that people can copy and paste the code instead of having to retype it.