Note: Nexus group repositories (good example in this StackOverflow question) are out of this tutorial's scope. In any case, deployment to group repositories is currently still an open issue for Nexus 3 (and not intended ever to be implemented in Nexus 2). Thus, it is assumed that we'll push & pull to/from the same repository, and ignore the idea of groups hereon in.
-
Ask your sysadmin for a username & password allowing you to log into your organistation's Nexus Repository Manager.
-
Test the login credentials on the Nexus Repository manager at: http://localhost:8081/nexus/#view-repositories (
localhostin our case is replaced by a static IP, and can only be connected to over VPN). If your organisation requires a VPN to connect to it, connect to that VPN before proceeding with this tutorial. -
Grab the repository path to your internal npm repo. In our case, it's of format 'npm' and type 'hosted'. It resembles the form: http://localhost:8081/nexus/content/repositories/npm-internal.
-
Grab the repository path to your mirror of the npm public repo. In our case, it's of format 'npm' and type 'group'. It resembles the form: http://localhost:8081/nexus/content/groups/npm-all
-
Ensure that your VPN connection is still alive! Otherwise when we come to publish (or
npm login, a convenience introduced for Nexus 3 users), that step step will not proceed. -
npm loginis not implemented for Nexus 2, wherein you have to manualy set up your.npmrcfile instead. We'll thus proceed with Nexus 2-compatible instructions: Create an.npmrcfile in your npm repository as follows (proceeding as if your name is 'John Smith' and you work for 'my-org'):
init.author.name = John Smith
init.author.email = [email protected]
# an email is required to publish npm packages
email[email protected]
always-auth=true
# This is the registry to PULL from, so you likely want it to be npm-all to
# get access to all npm repos, rather than npm-internal (private repos only).
registry=http://localhost:8081/nexus/content/groups/npm-all
# base64-encoding of the username-password pair for your Nexus Repository
# Manager. In this case: admin:admin123
_auth=YWRtaW46YWRtaW4xMjM=Note: If you have both Nexus 3 and an npm login-compatible username (eg. all-lowercase letters), instead of writing this whole file manually, you could run the command npm login --registry=http://localhost:8081/nexus/content/groups/npm-all, then providing the login details for your Nexus Repository Manager as prompted. In such case, again ensure that the VPN is still connected when running the command.
- Add the following lines to your
package.jsonas a top-level property to provide a publishing destination:
"publishConfig": {
"registry": "http://localhost:8081/nexus/content/repositories/npm-internal/"
}- Optionally add
publish-my-organdprepublishscripts to thepackage.jsontoo:
"scripts": {
"lib": "./node_modules/.bin/tsc --project tsconfig.json",
"publish-my-org": "npm publish --registry http://localhost:8081/nexus/content/repositories/npm-internal/",
"prepublishOnly": "npm run lib"
}-
Before publishing, ensure that your
package.jsonspecifies all the files you want to include in thefilesfield, and any you want to ignore, in the optional.npmignorefile. Delete any build files (andpackage-lock.jsonandnode_modules) carried over from another branch's previous checkout if relevant before building for publishing. Ensure the project's version number is up to date. -
Ensure your VPN connection is still active, and run
npm run publish-my-org. I accidentally rannpm publish, yet it mercifully appears to use the registry recorded inpublishConfigin such case, so there's no real necessity for thenpm run publish-my-orgscript apart from peace of mind.
- Add just the following line to your parent project's
.npmrc(no need for_authfield):
# For pulling
registry=http://localhost:8081/nexus/content/groups/npm-all/- Optionally set your parent project as private in the
package.json, to prevent it ever being accidentally published to the public registry:
"private": true- You can refer to your npm package in
dependencies(or any similar field) as you would any normal public npm package (we didn't use a scope):
"devDependencies": {
"my-internal-proj": "1.0.0"
}- You should require an active connection to your VPN in order to successfully perform
npm install.
-
Sonatype/Nexus Repository Manager 3/npm latest documentation
-
Sonatype/Nexus Repository Manager 2/npm legacy documentation
-
Using Nexus 3 as your repository official Sonatype blogpost.
-
Publishing to Nexus groups StackOverflow question (for example of how publishing command relates to registry).
-
.npmrc official NPM documentation
-
npm publish official NPM documentation
-
Working with private modules official NPM documentation