Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I login to yarn non-interactively?

When using npm, I can login non-interactively with:

$ printf "jesstelford\n<password>\[email protected]\n" | npm login

However, a similar command with yarn hangs:

$ printf "jesstelford\[email protected]\n" | yarn login
yarn login v0.21.3
question npm username: jesstelford
question npm email:

In interactive mode, I can successfully run:

$ yarn login
yarn login v0.21.3
question npm username: jesstelford
question npm email: [email protected]
✨  Done in 22.53s.

How can I run yarn login non-interactively?

like image 924
Jess Telford Avatar asked Apr 07 '17 05:04

Jess Telford


People also ask

Why is yarn command not found?

To solve the error "yarn: command not found", install the yarn package globally by running npm install -g yarn and restart your terminal. If the command fails, run it with sudo and make sure the correct PATH is set in your system's environment variable.

Do you need node to run yarn?

You will need to install Node.js if you don't already have it installed. On common Linux distributions such as Debian, Ubuntu and CentOS, it is recommended to install Yarn via our packages instead.


2 Answers

yarn appears to pause after the username is entered. You will also need to pause when in non-interactive mode:

$ { echo "jesstelford"; sleep 1; echo "[email protected]"; } | yarn login

This will give you the following output:

yarn login v0.21.3
question npm username: jesstelford
question npm email: [email protected]
✨  Done in 0.84s.

How it works

echo "jesstelford" enters the string, followed by a newline character

sleep 1 will insert a 1 second pause after entering the username, before continuing on to enter the email:

echo "[email protected]" enters the second string, followed by a newline character to end the command.

like image 70
Jess Telford Avatar answered Oct 18 '22 21:10

Jess Telford


I figured it out how to make yarn use nexus credentials in Dockerfile. If this can be done with nexus, I believe it will work for every other use case where it was problematic.

Here's the snippet:

# Install necessary package to login automatically
RUN npm install -g npm-cli-adduser

# Whenever auth changes, this has to be incremented,
# so following calls are not cached by Docker (or always build with --no-cache).
ARG FORCE_NO_CACHE_FLAG=2

# Authenticate to Nexus via npm package that does the job
RUN npm-cli-adduser -u ${NPM_USER} -p ${NPM_PASS} -e ${NPM_EMAIL} -r ${NPM_REGISTRY} -s ${NPM_SCOPE} -a

# Now, the fun / missing part I struggled with
# To fix yarn being silly aby not using token from npm-cli-adduser
# You need to set it explicitly.
# I found this by comparing my local `yarn config list` with a docker one.
# On local yarn was able to talk to nexus, not on docker. This piece one missing somehow.
RUN npm config set ${NPM_SCOPE}:registry ${NPM_REGISTRY}

# Now this will work, yarn will auth properly with your register.
RUN yarn install

This whole thing took me way too much. If there's anyone like me (or future me) with this problem, I hope this post will help.

like image 27
sznowicki Avatar answered Oct 18 '22 19:10

sznowicki