It looks like the heroku
CLI entry point is a batch file, as implied by the wording of the error messages, which are cmd.exe
's, not PowerShell's.
PowerShell doesn't take the special parsing needs of batch files (cmd.exe
) into account when it synthesizes the actual command line to use behind the scenes, which involves re-quoting, using double quotes only, and only when PowerShell thinks quoting is needed.
In this case PowerShell does not double-quote (because the value contains no spaces), which breaks the batch-file invocation.
You have the following options:
You can use embedded quoting so as to ensure that the value part of your MONGODB_URI=...
key-value pair is passed in double quotes; note the '"..."'
quoting:
heroku config:set MONGODB_URI='"mongodb+srv://myprojectname:<mypassword>@cluster0.rkitj.mongodb.net/<myusername>?retryWrites=true&w=majority"'
- Caveat: This shouldn't work, and currently only works because PowerShell's passing of arguments to external program is fundamentally broken as of PowerShell 7.1 - see this answer. Should this ever get fixed, the above will break.
If your command line doesn't involve any PowerShell variables and expressions, you can use --%
, the stop-parsing symbol, which, however, in general, has many limitations (see this answer); essentially, everything after --%
is copied verbatim to the target command line, except for expanding cmd.exe
-style environment-variable references (e.g., %USERNAME%
):
heroku config:set --% MONGODB_URI="mongodb+srv://myprojectname:<mypassword>@cluster0.rkitj.mongodb.net/<myusername>?retryWrites=true&w=majority"
If you're willing to install a module, you can use the ie
function from the PSv3+ Native
module (install with Install-Module Native
from the PowerShell Gallery in PSv5+), which internally compensates for all of PowerShell's argument-passing and cmd.exe
's argument-parsing quirks (it is implemented in a forward-compatible manner so that should PowerShell itself ever get fixed, the function will simply defer to PowerShell); that way, you can simply focus on meeting PowerShell's syntax requirements, and let ie
handle the rest:
# 'ie' prepended to an invocation that uses only PowerShell syntax
ie heroku config:set MONGODB_URI='mongodb+srv://myprojectname:<mypassword>@cluster0.rkitj.mongodb.net/<myusername>?retryWrites=true&w=majority'