Back to blog results

August 22, 2016By Chris Tozzi

Using Node.js npm with Artifactory via the API and CLI

The most common way to work with JFrog Artifactory is through the browser-based interface. But if you’re using Artifactory in conjunction with npm, the Node.js package manager, there are also handy interfaces available through both the npm command line and the Artifactory CLI.

In this post, I’ll go over the basics of using the npm CLI and the Artifactory CLI to work with npm packages and registries.

Why use npm and Artifactory via CLI or API?

First, let’s explain why you might want to do this. Again, Artifactory provides a rich, user-friendly Web interface. Usage procedures for the Artifactory browser interface are well documented.

But Web interfaces have downsides, of course. In particular, they’re hard to script. They also do not work well if you need to perform tasks from within an application using an API.

More importantly, if you are coming to Artifactory from the npm world rather than vice versa, you are likely to find it easier to work using the npm CLI tools that you already know.

How to use Artifactory and the npm CLI

Now, let’s take a look at how to get started working with Artifactory from the npm command line. (I’ll get to the API below.)

Installation and setup

First, of course, you should install npm on your system. It’s part of Node.js, so you’ll want to follow the instructions for setting up Node on your platform of choice.

Once npm is installed on your system, you will also need to create an npm repository in Artifactory. You can do this using the Set Me Up tool, which is available from the Tree Registry. For details on using Set Me Up, check out the Artifactory documentation here.

You will also need to tell npm where to find your Artifactory npm repository using the following command:

npm config set registry

Usage

With npm installed on your system and an npm repo configured in Artifactory, you can use the npm CLI tool to execute npm commands to your heart’s desire. (There are just a few exceptions—in particular, Artifactory does not support npm’s adduser command or the -tag argument for npm publish.)

The full list of npm commands available are far too extensive to list here, but some quick examples of what you might want to do if you’re getting started and poking around your Artifactory repo include:

npm search

This command will search for packages in your repo based on the package names and your search terms.

npm install

This installs a package from your repo. For a fuller (but not exhaustive) guide to npm CLI commands, this npm cheat sheet is handy.

Authentication

In order to perform certain operations, such as publishing packages, the npm CLI will require you to authenticate. To make this possible, first add the following information to your .npmrc file (on Linux, that should be in your user’s /home directory):

  • Your Artifactory username and password in the form username:password in Base64 encoding
  • Your email address

You should also set the always-auth variable to true.

Again, authentication is not required for all operations (so long as Artifactory is configured to allow anonymous access).

How to use npm with the Artifactory API

You can also use the Artifactory API to work with your Artifactory npm repo. To set this up, first install the requisite npm package with:

npm install artifactory-api

You can then use Artifactory API requests (which are documented here) to interact with the repo. Here again, the list of available options is much too long to detail in full, but the npm community has a useful list of basic examples.

The one item to keep in mind is that, as with the npm CLI, API requests require authentication when creating a new instance. You also send these in the form of username:password encoded in Base64. For example:

var artifactory = new ArtifactoryAPI('https:', "dXNlcjpwYXNzd29yZA==");

Once authenticated, you can take advantage of the Artifactory API to make your life simpler and more automated, at least when it comes to working with your Artifactory npm repo.

It’s worth noting that Sumo Logic provides an App for JFrog Arifactory so that existing customers can get real-time analytics on th Artifactory instances.. In addition, JFrog Artifactory users can access a free version of Sumo Logic directly from Artifactory’s admin interface (under advanced options. This Webinar walks you through the integration. For those looking to go a little deeper,JFrog’s Co-founder and Chief Architect talks with Sumo Logic’s Chief Architect, Stefan Zier about the integration and how the dashboards were designed.

Using Node.js npm with JFrog Artifactory via the API and CLI is published by the Sumo Logic DevOps Community. If you’d like to learn more or contribute, visit devops.sumologic.com. Also, be sure to check out Sumo Logic Developers for free tools and code that will enable you to monitor and troubleshoot applications from code to production.

Complete visibility for DevSecOps

Reduce downtime and move from reactive to proactive monitoring.

Chris Tozzi

Chris Tozzi

Chris Tozzi is a member of the Sumo Logic Community and has worked as a journalist and Linux systems administrator. He has particular interests in open source, agile infrastructure and networking.

More posts by Chris Tozzi.

People who read this also enjoyed