Allowing "at" characters in npm usernames

#javascript #npm #proget #stackoverflow

Written by Anders Marzi Tornblad

This is part 1 of the Email usernames in npm series.

StackOverflow user EtherealVoid was having problems with npm usernames containing @ characters in a private npm registry. They tried npm adduser, scl and npm login, but could not get npm to accept an email address as the username, because it contains the @ character. Escaping @ as %40 also did not help.

npm WARN Name may not contain non-url-safe chars

I had the same problem at work when using Proget from Inedo as our main npm registry. We used our work email addresses as usernames, which was rejected by npm adduser, because is not supported by the npm command line tools. You need a way to provide your user name without running npm adduser or npm login.

If you are using a private registry, and you are using scopes to separate the different registries, you can create a .npmrc that looks like this:

# Settings for @SCOPENAME
@SCOPENAME:registry=https://DOMAIN/FEED/
//DOMAIN/FEED/:username=USER@DOMAIN
//DOMAIN/FEED/:_password=BASE64(PASSWORD)
//DOMAIN/FEED/:email=EMAIL_ADDRESS
//DOMAIN/FEED/:always-auth=true

# Settings for @OTHERSCOPENAME
@OTHERSCOPENAME:registry=...

I couldn't get this trick to work properly without the trailing / in the //DOMAIN/FEED/, but that might depend on your registry's prefeered feed urls. Try both with and without trailing slash.

It's also possible to set the username and password for your default scope:

# Settings for default scope
registry=https://DOMAIN/FEED/
//DOMAIN/FEED/:username=USER@DOMAIN
//DOMAIN/FEED/:_password=BASE64(PASSWORD)
//DOMAIN/FEED/:email=EMAIL_ADDRESS
//DOMAIN/FEED/:always-auth=true

Articles in this series: