I'm trying to write an automation Python script and I need to perform npm login as one of the steps. The npm login command prompts for password. I thought I could send it as a standard input, but it doesn't work:
proc = subprocess.run(['npm', 'login'],
encoding='utf-8',
text=True,
input='honzajavorek\npassword\n')
print(proc)
The code above exits with the following output:
Username: honzajavorek
Password: npm ERR! cb() never called!
npm ERR! This is an error with npm itself. Please report this error at:
npm ERR! <https://npm.community>
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/honza/.npm/_logs/2020-09-02T13_45_56_798Z-debug.log
CompletedProcess(args=['npm', 'login'], returncode=1)
I thought I need to use Popen and be more interactive, but it actually got even worse. In the output above, you can see at least my username got entered, but the following code won't even manage that:
proc = subprocess.Popen(['npm', 'login'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf-8',
text=True,
stdin=subprocess.PIPE)
proc.stdin.write('honzajavorek\n')
proc.stdin.flush()
proc.stdin.write('password\n')
proc.stdin.flush()
stdout, stderr = proc.communicate()
print(stdout, stderr)
What am I doing wrong?