Install npm packages from CloudRepo
npm reads CloudRepo like any other npm registry: point npm at the repository, give it a token, and install. This page covers installing packages. To publish packages, see Publish npm packages.
You need an npm repository. If you have none yet, create a repository.
Point npm at the repository
Section titled “Point npm at the repository”There are two ways to sign npm in. On your own machine, npm login signs you in through your
browser and saves a token for you, so there is nothing to copy. In CI, a token kept in an
environment variable signs npm in, with no browser.
On your machine: npm login
Section titled “On your machine: npm login”Run npm login with your repository’s URL, ending in /:
npm login --registry https://your-org.mycloudrepo.io/repositories/your-repo/A browser opens. Sign in to CloudRepo as you do in the admin portal, then click Authorize. npm saves the token in ~/.npmrc for you. It is read-only unless you choose Read-write, and read-only is enough to install. It works only for this repository, expires after 90 days, and appears on the Repository Tokens page where you can revoke it (more in npm login).
Run npm login against a Hosted npm repository. A Proxy npm repository or a group does not take the
token npm login saves: use a repository token there, as in CI below.
Then send your scope to the repository, in the .npmrc beside your package.json. It needs no token
line: npm reads the token from ~/.npmrc.
@yourscope:registry=https://your-org.mycloudrepo.io/repositories/your-repo/In CI: a token in an environment variable
Section titled “In CI: a token in an environment variable”You need a repository token that reaches the repository; a token that has Download packages access is enough to
install. If you have none, create a token. Keep it in
your CI’s secret store as the environment variable NPM_TOKEN.
Put this in the .npmrc beside your package.json. It sends the packages of your own scope to
CloudRepo and everything else to the public npm registry, so no public package has to be in your
repository.
@yourscope:registry=https://your-org.mycloudrepo.io/repositories/your-repo///your-org.mycloudrepo.io/repositories/your-repo/:_authToken=${NPM_TOKEN}- Replace
@yourscopewith the scope of your private packages. - npm replaces
${NPM_TOKEN}with the environment variable of that name, so the file can be committed without the token in it. - Keep the trailing
/on both lines, and keep the second line identical to the registry URL withouthttps:. npm sends the token only to the registry that line names. - npm sends the token alone, as
_authToken. There is no username.
The repository URL is https://<organization>.mycloudrepo.io/repositories/<repository>/: your
organization’s name, then the repository’s name as the admin portal shows it.
Check that CloudRepo accepts the token:
npm whoami --registry=https://your-org.mycloudrepo.io/repositories/your-repo/Expected: npm prints the email address of the account that created the token. A token that is mistyped, expired or revoked fails here (see When npm answers 401, 403 or 404).
Install a package
Section titled “Install a package”npm install @yourscope/my-packageExpected: npm adds the package to node_modules and to package.json.
A package without a scope can live in a CloudRepo repository too, but npm sends only a scope to a
second registry, so give your private packages a scope and use the @yourscope:registry= line. A hosted
repository holds only what was published to it, so a registry= line that sends every package there
makes every public package not found. To install public packages through CloudRepo as well, create an
npm proxy repository and use that as the registry.
npm groups (early access)
Section titled “npm groups (early access)”An npm group gives npm one registry URL for several of your npm repositories: your hosted repositories and proxy repositories of the public npm registry. npm installs packages held by the group’s hosted repositories, and public npm packages, from that one URL. npm groups are in early access.
Create the group. In the admin portal, create a repository with npm as the Format and Group as the Kind. Open the group’s settings and choose Manage Group Members: your hosted and proxy npm repositories are listed. Turn on Is Member for each repository the group should serve, and give each a Priority. A group holds at most 10 repositories. To install public packages through the group, include an npm proxy repository of the public npm registry.
Point npm at the group. Use the group’s URL where the repository’s URL went above, with the trailing
/ on both lines:
@yourscope:registry=https://your-org.mycloudrepo.io/repositories/npm-group///your-org.mycloudrepo.io/repositories/npm-group/:_authToken=${NPM_TOKEN}Here the group is named npm-group; put your group’s name in the URL. With the @yourscope:registry= line, the
packages of your scope come from the group and every other package comes from the public npm registry. Use
registry= in place of that line only for a group that includes a proxy of the public npm registry. Without one,
keep the @yourscope:registry= line: with registry=, every public package would answer 404.
Run npm audit against your npm proxy repository, with a token that ticks the proxy. Add the proxy’s token line
to .npmrc, //your-org.mycloudrepo.io/repositories/npm-proxy/:_authToken=${NPM_TOKEN}, then run
npm audit --registry=https://your-org.mycloudrepo.io/repositories/npm-proxy/.
Then install, as before:
npm install @yourscope/ui @yourscope/utilsExpected: npm installs @yourscope/ui and @yourscope/utils through the group’s one URL. If two of your
hosted repositories hold the same package name, the one with the lowest Priority number serves it, and
the group does not mix their versions.
A group takes a repository token, not npm login. Create the token as
Repository tokens shows, and tick the group. Through the group’s
URL, the token reads the group’s current repositories, read only. The form ticks each repository with the
group, so the token can also use a repository’s own address, such as to publish to it. npm login does not
work on a group.
A group is read only. Publish to one of its repositories, by that repository’s own address.
npm publish to the group’s URL is refused. Public (unauthenticated) downloads are not served for npm
groups yet.
When npm answers 401, 403 or 404
Section titled “When npm answers 401, 403 or 404”npm sends _authToken as a Bearer token. A token that is mistyped, expired, revoked or from another
organization answers 401, and npm prints Your token wasn't accepted. Run npm login or check the token in your .npmrc. If npm reports 404 for a package you know exists, check the token first,
and then whether the token reaches the repository (a proxy repository it does not reach answers 404).
Check these in order:
- The token line. Its
//your-org.mycloudrepo.io/repositories/your-repo/:prefix must match the registry URL exactly, withouthttps:and with the trailing slash. - The token. Paste it whole, starting at
crp_v1_, orcrk_v1_for an organization token, and check on the Repository Tokens page that it has not expired or been revoked. - The repository. A token reaches only the repositories ticked when it was created. A hosted
repository it does not reach answers
403 Forbidden, and a proxy repository it does not reach answers404 Not Found, not403: if you installed through a proxy and got a404with a good token, check that the token includes the proxy.For a group, ticking the group is enough to read every repository in it through the group’s URL; a repository’s own address needs that repository ticked too.
- The scope. If
npm install @yourscope/my-packageanswers404and the token is good, npm is looking at the public registry: the@yourscope:registry=line is missing, or names another scope. - An
npm logintoken. A token fromnpm login(crn_v1_) works only on the hosted npm repository you logged in to. A proxy npm repository or a group refuses it with403. Use a token from Repository Tokens there. - A publish to a group. A group is read only, so
npm publishto its URL is refused: with403 Forbiddenfor a read-only token, and with405 Method Not Allowedfor a token that can write. Publish to one of its repositories, by that repository’s own address.
More: Repository tokens, for every client’s credential
and the npm login browser flow; Proxy repositories;
npm repositories, for publishing, size limits and unpublishing.