Skip to content

Publish npm packages to CloudRepo

npm publishes to CloudRepo the way it publishes to any registry: npm signs in, and npm publish uploads the package. This page is for the person who publishes a package. To install it, see npm repositories.

  • A Hosted npm repository. If you have none, create one.
  • A way to sign in that can write to it: on your machine, npm login with Read-write chosen; in CI, a repository token that reaches it, with Download and publish packages. A Download packages token can install but not publish: CloudRepo answers its npm publish with 403. See Repository tokens to create one. npm sends the token on its own, as _authToken. There is no username.
  • Your repository’s registry URL, https://<organization>.mycloudrepo.io/repositories/<repository>/, with the trailing slash. Your organization’s name is the first part of your CloudRepo host, and the repository’s name is the one the admin portal shows.
  • A package name in lowercase letters, digits, hyphens, dots and underscores, scoped (@scope/name) or not.

Used on this page only, never saved. Never enter a token or password here.

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.

Run npm login with your repository’s URL, ending in /:

Terminal
npm login --registry https://your-org.mycloudrepo.io/repositories/your-repo/

A browser opens. Sign in to CloudRepo there, the way you sign in to the admin portal, choose Read-write on the Authorize npm CLI page, and click Authorize. The default, Read-only, cannot publish, and Read-write needs write access to the repository. npm saves the token in ~/.npmrc for you. It reaches only this repository, expires after 90 days, and is listed on the Repository Tokens page, where you can revoke it (more in npm login).

Run npm login against a Hosted npm repository, since only Hosted repositories accept publishes. Then go to your package directory and publish to the same URL:

Terminal
cd my-package
npm publish --registry https://your-org.mycloudrepo.io/repositories/your-repo/

1. Put your scope and the credential in ~/.npmrc. npm replaces ${NPM_TOKEN} with the environment variable of that name, so the file holds no token and can be copied or committed. Export the variable in the shell that runs npm, or in your CI’s secret store.

~/.npmrc
@yourscope:registry=https://your-org.mycloudrepo.io/repositories/your-repo/
//your-org.mycloudrepo.io/repositories/your-repo/:_authToken=${NPM_TOKEN}

Send your scope’s packages to this repository; every other package still comes from the public npm registry. Replace @yourscope with your scope.

Keep the trailing / on both lines, and keep the second line identical to the registry URL without https:. npm sends the token only to the registry that prefix names, so if the two differ, even by the slash, npm sends no token at all.

2. Check the connection.

Terminal
npm whoami --registry https://your-org.mycloudrepo.io/repositories/your-repo/

Expected: npm prints your email address.

3. Publish. Run this in the directory of your package:

Terminal
npm publish --registry https://your-org.mycloudrepo.io/repositories/your-repo/

Expected: npm prints + <name>@<version>. The publish names the repository, so a package without your scope won’t go to the public registry.

4. Check that it landed.

Terminal
npm view docs-npm version --registry https://your-org.mycloudrepo.io/repositories/your-repo/

Expected: npm prints the version you published. The package is also listed in the repository in the admin portal.

A scope routes a package name to a registry. Name your package with your scope: the @yourscope:registry= line of step 1 sends @yourscope/* packages to CloudRepo, and every other package to npm’s default registry. A scoped package needs this line to install, too: if npm install @yourscope/name answers 404, npm is asking the default registry, and the line is missing.

Publish to CloudRepo and install from elsewhere

Section titled “Publish to CloudRepo and install from elsewhere”

If npmjs.org stays your default registry, name CloudRepo in the package itself. publishConfig in package.json makes npm publish go there, whatever your default registry is. The token line stays in ~/.npmrc:

package.json
{
"name": "my-package",
"version": "1.0.0",
"publishConfig": {
"registry": "https://your-org.mycloudrepo.io/repositories/your-repo/"
}
}
~/.npmrc
//your-org.mycloudrepo.io/repositories/your-repo/:_authToken=${NPM_TOKEN}

Store the token in your CI’s secret store, expose it as NPM_TOKEN, and reference it in .npmrc as in step 1. Then run the publish command from step 3:

Terminal
npm publish --registry https://your-org.mycloudrepo.io/repositories/your-repo/

To write the .npmrc at build time, from the environment:

Terminal
echo '//your-org.mycloudrepo.io/repositories/your-repo/:_authToken=${NPM_TOKEN}' >> .npmrc

The single quotes matter: they write the text ${NPM_TOKEN} into the file, and npm replaces it with the variable’s value when it runs, so the file never holds the token.

Never commit a token. Commit only the .npmrc that names the variable, and keep the variable itself in your CI platform’s secrets (GitHub Actions secrets, GitLab CI variables, Jenkins credentials).

CloudRepo accepts an npm publish of up to 210 MiB (220,200,960 bytes), which holds a tarball of about 155 MiB: npm sends the tarball inside a JSON document, and base64 encoding adds about a third. A larger publish is refused with 413. If you need to publish larger packages, split them into several.

Overwrite Protection is the default on every hosted npm repository. With it on, publishing a version that is already in the repository is refused with 409 Conflict, and the version already there stays as it is. npm prints CloudRepo’s explanation: Version already exists. Overwrites are disabled for this repository.

Increment the version in package.json and publish again. If the repository is meant to accept republished versions, turn Overwrite Protection off in its settings instead; see Overwrite Protection. npm has no mutable-version convention, so there is no equivalent of Maven’s -SNAPSHOT: no version is exempt.

Check these in order:

  • ENEEDAUTH, or “need auth”, and no request reached CloudRepo. npm sent no token. The //your-org.mycloudrepo.io/repositories/your-repo/: prefix on the _authToken line must match the URL you pass to --registry exactly, without https: and with the trailing slash.
  • 401 Unauthorized. npm sends _authToken as a Bearer token, and CloudRepo refuses a token it does not accept (mistyped, expired, revoked or from another organization): npm prints Your token wasn't accepted. Run npm login or check the token in your .npmrc. Check the variable is set (see above), that the token is whole, and that it has not expired: the Repository Tokens page in the admin portal shows its status.
  • 404 Not Found. Check the token first, as for a 401, then that the --registry URL names your repository.
  • 403 Forbidden. The token cannot publish here. A Download packages token cannot publish at all, and tokens only work for the repositories selected when they were created. Use a Download and publish packages token that includes this repository. A token from npm login is also read-only unless you chose Read-write when authorizing it.
  • 409 Conflict. The version is already in the repository. See above.
  • 413. The publish is over 210 MiB (220,200,960 bytes). See above.

More: Repository tokens, for every client’s credential; and npm repositories, for installing, npm login, and taking a version back.

View as Markdown