Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Running examples

David Recuenco edited this page Jan 26, 2021 · 6 revisions

Running examples

We recommend spinning up an instance of sonatype nexus3 for development purposes.

This guide is a short summary of a sonatype blog post by Rafael Eyng that you can read here.

Spin up nexus

With docker running in the background, we can launch nexus by simply running:

docker run -dit --name nexus -p 8081:8081 sonatype/nexus3

To login we can use the default admin credentials admin/admin123.

Note: if the login is invalid, the password has to be read from the nexus Docker volume:

docker exec -it nexus sh
cat /nexus-data/admin.password

Once the password is set, the wizard will ask for a new password (our recommendation is to use admin123 to be able to reuse the auth value of the .npmrc configuration file).

Setting up a private npm registry

In nexus, navigate to #/admin/repository/repositories

We will need three registries:

  • npm-private: a registry where our private packages will be stored.
  • npm-proxy: a registry that will mirror the public npm registry.
  • npm-virtual: a registry which will aggregate our private registry and the proxy.

Click Create repository and look for npm (hosted), this will be our private registry – let's name it npm-private. You can leave all other options as default, but consider changing Deployment policy to Allow redeploy, it will be helpful to update existing versions with our latest local changes.

After saving, click Create repository once more and look for npm (proxy), this will be our mirror of the public registry – let's name it npm-proxy. Set the Remote storage url to https://registry.npmjs.org, all other options can be left as-is.

Last but not least, click Create repository and look for npm (group), this will be our target registry to fetch packages from our private registry and the public registry – let's name it npm-virtual. All we need to do is select the two registries we just created from Availables and add them to Members.

In order to work with the registry, we will need to create a .npmrc file like the following:

registry=http://localhost:8081/repository/npm-private/
email=admin@example.com
_auth=YWRtaW46YWRtaW4xMjM=
always-auth=true

Notice _auth is admin:admin123 encoded in base64. It can be achieved in multiple ways: On browser console: btoa('admin:admin123'). On node: Buffer.from('admin:admin123').toString('base64'). In linux with openssl: echo -n 'admin:admin123' | openssl enc -base64.

It should be located either at the root of the project or at $HOME/.npmrc.

Publishing against the private registry

To check which packages would be published in dry run mode, we can run:

npm run lerna:publish -- --dry-run

Additionally, if we want to publish against another registry, we can specify so by running:

npm run lerna:publish -- --registry http://localhost:8081/repository/npm-private/
# or
npm run lerna:publish:dev

If we want to override existing versions, we can use the force flag, e.g:

npm run lerna:publish -- --force --registry http://localhost:8081/repository/npm-private/ 

Publishing a single package

A single package can be published directly adding the registry as command line parameter:

npm publish --registry=http://localhost:8081/repository/npm-private/

Running the examples

We can run the following command to install dependencies and run scripts from the examples:

npm run examples

Under the hood it will run:

  • reinstall:examples: to reinstall all dependencies (fetching fresh versions from nexus) .
  • run:examples: run main scripts for each directory under examples/.