NPM¶
This repository is used for storing npm packages.
Use npm scope to work with a private repository.
LOC
in command examples
In command examples replace $LOC
with the currently selected ICDC location.
npm login¶
Use npm login
to authenticate with private repository and store credentials permanently in npm UserConfig
* file:
~/.npmrc`
npm login --repository=https://artifactory.$LOC.icdc.io/repository/ACC_NAME-npm --scope=@EXAMPLE --always-auth
Username: USERNAME
Password: PASSWORD
Email: EMAIL
npm config set @EXAMPLE:registry https://artifactory.$LOC.icdc.io/repository/ACC_NAME-npm
If your Username contains the @
symbol and you are having trouble logging in, use this script to log in, replacing ACC_NAME
with your account name:
npm-login.sh
function usage() {
[ -n "$1" ] && echo "Error: $1"
echo "Usage: $(basename $0) --registry=url|hostname --scope=@orgname" 1>&2;
exit 1;
}
while getopts ":-:" o; do
case "${o}" in
-)
case "${OPTARG}" in
registry) registry="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 )) ;;
registry=*) registry=${OPTARG#*=} ;;
scope) scope="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 )) ;;
scope=*) scope=${OPTARG#*=} ;;
esac;;
*) usage ;;
esac
done
shift $((OPTIND-1))
# Validate arguments
[ -z "$registry" ] && usage
is_url=$(echo "$registry" | grep -E "^https?://")
[ -z "$is_url" ] && registry="https://$registry/repository/ACC_NAME-npm"
[ -z "$scope" ] && usage
is_scope=$(echo "$scope" | grep -e "^@")
[ -z "$is_scope" ] && scope="@$scope"
# Prompt like standard 'npm login'
echo -n "Username: "; read username
echo -n "Password: "; read -s password; echo ""
is_email=$(echo "$username" | grep "@")
[ -n "$is_email" ] && email_prompt=" ($username)"
echo -n "Email${email_prompt}: "; read email
[[ -z "$email" && -n "$is_email" ]] && email=$username
login_url="$registry/-/user/org.couchdb.user:$username"
TOKEN=$(curl -g -s -H "Accept: application/json" -H "Content-Type:application/json" -X PUT --data "{\"name\": \"$username\", \"password\": \"$password\", \"email\":\"$email\"}" "$login_url" 2>&1 | awk -v RS=',"' -F: '/^token/ {print $2}' | sed 's/["}]//g')
[ -z "$TOKEN" ] && echo "npm ERR! 401 Unauthorized - PUT $login_url - Bad username or password" 1>&2 && exit 2
npm_registry=$(echo "$registry" | sed -e 's/^http[s]*://')
npm_registry=$(echo "$npm_registry" | sed -e 's/ACC_NAME-npm/ACC_NAME-npm\//g')
registry=$(echo "$registry" | sed -e 's/ACC_NAME-npm/ACC_NAME-npm\//g')
npm set "$scope:registry" "$registry"
npm set "$npm_registry:_authToken" "$TOKEN"
echo "Logged in as $username on $registry"
npm publish¶
Use npm scope to publish the package.
Add your scope name into package.json
:
Then npm publish
to upload package into repository:
npm install¶
To install some package from your private repository specify scope: