{"id":70,"date":"2017-03-29T16:53:12","date_gmt":"2017-03-29T08:53:12","guid":{"rendered":"http:\/\/www.jsjs.org\/?p=70"},"modified":"2017-03-29T16:53:12","modified_gmt":"2017-03-29T08:53:12","slug":"using-nexus-3-as-your-repository-part-2-npm-packages","status":"publish","type":"post","link":"https:\/\/blog.jsjs.org\/?p=70","title":{"rendered":"Using Nexus 3 as Your Repository \u2013 Part 2: Npm Packages"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-20594\" src=\"http:\/\/www.sonatype.org\/nexus\/content\/uploads\/2014\/10\/NPM-Logo.jpg\" alt=\"NPM\" width=\"590\" height=\"240\" \/><\/p>\n<p><em>This is the second part of a series of posts on Nexus 3 and how to use it as repository for several technologies. Also available is <a href=\"http:\/\/www.sonatype.org\/nexus\/2017\/02\/08\/using-nexus-3-as-your-repository-part-1-maven-artifacts\/\" target=\"_blank\" rel=\"noopener\">\u201cPart 1, Maven Artifacts\u201d<\/a> by Rafael Eyng.<\/em><\/p>\n<p><code class=\"highlighter-rouge\">npm install<\/code> can take too long sometimes, so it might be a good idea to have a proxy in your own network. And if you can\u2019t just pay the 7 dollars\/month to host your packages in the official npm private registry, then you\u2019ll probably benefit from this post.<\/p>\n<h2 id=\"installation\">Installation<\/h2>\n<p>Check out the <a href=\"http:\/\/codeheaven.io\/using-nexus-3-as-your-repository-part-1-maven-artifacts\/\">first part<\/a> of this series to see how we installed and ran Nexus 3 using a single docker command. Just do that and the installation is done.<\/p>\n<h2 id=\"configuring-nexus-as-a-npm-repo\">Configuring Nexus as a npm repo<\/h2>\n<p>What we will do:<br \/>\n\u2013 create a private (hosted) repository for our own packages<br \/>\n\u2013 create a proxy repository pointing to the official registry<br \/>\n\u2013 create a group repository to provide all the above repos under a single URL<\/p>\n<p>I suggest you to create a new blob store for each new repo you want to create. That way, the data for every repo will be in a different folder in <code class=\"highlighter-rouge\">\/nexus-data<\/code> (inside the Docker container). But this is not mandatory for it to work.<\/p>\n<h3 id=\"private-repo\">private repo<\/h3>\n<p>A repository for npm packages that your team develops.<\/p>\n<p>Create a new npm (hosted) repository and configure it like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-63458\" src=\"http:\/\/www.sonatype.org\/nexus\/content\/uploads\/2017\/02\/using-nexus-as-repository-part-2-1.png\" alt=\"using nexus as repository part 2 \" width=\"590\" height=\"421\" \/><\/p>\n<p>The deployment policy \u201cAllow redeploy\u201d above might look somewhat polemic, so you might want to set it to \u201cDisable redeploy\u201d. In my use case, it makes sense to use \u201cAllow redeploy\u201d, since we keep a <code class=\"highlighter-rouge\">latest<\/code> version on Nexus always updated with the status of the master branch, that is redeployed in our CI flow.<\/p>\n<h3 id=\"proxy-repo\">proxy repo<\/h3>\n<p>A repository that proxies everything you download from the official npm registry. Next time you download the same dependency, it will be cached in your Nexus.<\/p>\n<p>Create a new npm (proxy) repository and configure it like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-63460\" src=\"http:\/\/www.sonatype.org\/nexus\/content\/uploads\/2017\/02\/using-nexus-as-repository-part-2-2.png\" alt=\"using nexus as repository part 2\" width=\"590\" height=\"480\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-63467\" src=\"http:\/\/www.sonatype.org\/nexus\/content\/uploads\/2017\/02\/part-2-2.png\" alt=\"using nexus as your repository\" width=\"590\" height=\"449\" \/><\/p>\n<h3 id=\"group-repo\">group repo<\/h3>\n<p>This will group all the above repos and provide you a single URL to configure your clients to download from\/deploy to.<\/p>\n<p>Create a new npm (group) repository and configure it like:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-63459\" src=\"http:\/\/www.sonatype.org\/nexus\/content\/uploads\/2017\/02\/using-nexus-as-repository-part-2-3.png\" alt=\"using nexus as repository part 2 3\" width=\"590\" height=\"594\" \/><\/p>\n<p>You can create as many repos as you need and group them all in the group repo, but for npm I don\u2019t think that you will need more than 1 proxy and 1 private repos.<\/p>\n<h2 id=\"configuring-your-clients-and-projects-to-use-your-nexus-repos\">Configuring your clients and projects to use your Nexus repos<\/h2>\n<p>For npm, we will configure the repository per project (unlike Maven, that have some global configs, for instance). I believe that you can configure the authentication globally in your machine, with <code class=\"highlighter-rouge\">npm addUser<\/code>, but I didn\u2019t went that way for simplicity.<\/p>\n<p>If you have a project where you only want to <strong>download<\/strong> dependencies from Nexus, create a <code class=\"highlighter-rouge\">.npmrc<\/code> file at your project\u2019s root with:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>registry=http:\/\/your-host:8081\/repository\/npm-group\/\n_auth=YWRtaW46YWRtaW4xMjM=\n<\/code><\/pre>\n<\/div>\n<p><code class=\"highlighter-rouge\">_auth=YWRtaW46YWRtaW4xMjM=<\/code> is the base64 hash for the credentials (admin\/admin123). If you use a different set of credentials, you should compute your own hash with:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>echo -n 'myuser:mypassword' | openssl base64\n<\/code><\/pre>\n<\/div>\n<p>You have to set a user so you can publish packages. If you do this from your local machine, <code class=\"highlighter-rouge\">npm publish<\/code> will use your user configured in <code class=\"highlighter-rouge\">~\/.npmrc<\/code> (in your home, not in your project). If you don\u2019t have this configuration, or if you want to publish from CI, you can set an <code class=\"highlighter-rouge\">email=any@email.com<\/code> configuration in your project\u2019s <code class=\"highlighter-rouge\">.npmrc<\/code>. Really, any email.<\/p>\n<p>If you have a project that you want to <strong>publish<\/strong> to your Nexus, put this in <code class=\"highlighter-rouge\">package.json<\/code>:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code><span class=\"p\">{<\/span>\n  <span class=\"err\">...<\/span>\n\n  <span class=\"nt\">\"publishConfig\"<\/span><span class=\"p\">:<\/span> <span class=\"p\">{<\/span>\n    <span class=\"nt\">\"registry\"<\/span><span class=\"p\">:<\/span> <span class=\"s2\">\"http:\/\/your-host:8081\/repository\/npm-private\/\"<\/span>\n  <span class=\"p\">}<\/span>\n<span class=\"p\">}<\/span>\n<\/code><\/pre>\n<\/div>\n<p>Note that you publish to your private repo, but when you download, you can point to your group repo, so both your own packages and the packages from the official repo will be available from a single URL.<\/p>\n<p>Now if you run in your projects:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>npm install\n# or\nnpm publish\n<\/code><\/pre>\n<\/div>\n<p>your <code class=\"highlighter-rouge\">npm<\/code> will point to your Nexus instance.<\/p>\n<h2 id=\"installing-npm-packages-globally\">Installing npm packages globally<\/h2>\n<p>Run:<\/p>\n<div class=\"highlighter-rouge\">\n<pre class=\"highlight\"><code>npm --registry http:\/\/your-host:8081\/repository\/npm-group\/ install -g your-pac<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is the second part of a series of posts on Nexus 3 and how to use it as repository [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=70"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}