Local (offline) npm repository


Background


Deciding to sell out at a high price, I ended up at the employer, where there is no Internet not only in the industrial circuit, but also in the maiden (the situation is actually not uncommon in many, so to speak, "enterprises"). There is IE in the console where you, my dear reader, can of course look at Habr or stackoverflow, but no more. In general, I repeat, the situation is not exceptional, you can live with it - here it is, my personal laptop on which I write it, is on the table ... is it wifi lifted by many employees, is it cellular as a modem, in general, if it's not a defense / any services of three letters (here I advise you to go by official-expensive ways, as will be a couple of words below) and there is no radio monitoring service, then you are free to do anything with your personal laptop (within the permissible :)), well, at least that's how it looks with us. The ability to drag and drop files into the dev environment is, at the very least, but I really didn’t like the decision to keep node_modules in the codebase and I rolled up my sleeves (I rolled up my sleeves many times both about moving from TFS to git, and about the local nuget repo, but these are separate stories, also not without drama, but about them sometime next time). There are several rather expensive solutions to the issue, it is possible that I could even push through some Artifactory, but when I imagined how much this would take and most importantly, how long it would be decided, coordinated and implemented, it was decided to act cheaply and angrily.


Disclaimer: since I had a windows environment, I describe in detail exactly the work in the Windows console, however, in essence, the differences will only be at the end when you want to raise the final result as a service / daemon.


Not Tomi, soulmate, how?


In action involved:


  • A computer with internet access - call it source
  • A computer without access, on a corporate network - call it the target machine

There must be node.js on the source and on the target machine (the easiest way is if one or similar versions).


Create a daddy on the source, somewhere and call it, for example, verdaccio:


mkdir verdaccio
cd verdaccio

After locally installing Verdaccio, this is a very useful thing, based on the old sinopia project. Read more here: https://verdaccio.org/ .


In short, this is a proxy for npm, which can cache. For so many users of this package, the key opportunity is the ability to publish privately, my case is somewhat different, I wanted to cache everything I need for work and a little more, and then transfer it all to the target machine. Looking ahead, I’ll say that everything went pretty painlessly, except for the transfer of node-sass. It was the root of her tarballs that I spent a day and a half (there is a native part that is loaded from the github during the installation process or is going to gyp, if it suddenly didn’t work out - think about it, it’s going to be built on each local development machine, which is completely uncomfortable), but it’s probably another story too.


Everything is simple here:


npm i verdaccio

In general, the manual recommends installing globally, but in fact this is not necessary at all, moreover, we want to localize all this disgrace in one folder, so that later it can be conveniently copied. Because so ...


Run the package, it will create the default storage and configuration:


.\node_modules\.bin\verdaccio.cmd


Then just everyone told us where the config is, on which port we are hanging.


In principle, you can already go to the specified address and see something like this:



Now we need to do a few things:


  • Clear npm-cache ( more details here , in short, kill the contents of % AppData% / npm-cache )


  • Run


    npm set registry http://localhost:4873/

  • Copy config.yaml to our cozy verdaccio folder


  • Make sure the uplinks section of this file looks like this:


    uplinks:
    npmjs:
      url: https://registry.npmjs.org/
      cache: true

  • At the end of the config add:


    listen:
      - 0.0.0.0:4873

    If this is not done, then you will not be able to reach the local address later on the target machine.


  • Actually we are ready, now all the packages that we take through npm install will settle in ./storage, which is what we need. We restore the target project, set additionally:



npm i node-windows

That's all, after that we take the folder and send it to the target machine in any way possible for you ( admin rights are needed there , yes). You can already there, you can immediately create a file in the root of the installservice.js folder:


var Service = require('node-windows').Service;
var svc = new Service({
  name: 'VerdaccioSvc',
  description: 'Verdaccio - npm local repository',
  script: ' <тут путь до папки>\\node_modules\\verdaccio\\bin\\verdaccio',
  nodeOptions: []
}); 
svc.on('install', function() {svc.start();});
svc.install();

We launch:


node installservice.js

Enjoy!


In general, we managed to get a local repository with pretty little blood, builds are building, developers are developing everything in this world in their places. And yes, subsequent synchronization comes down to copying the contents of ./storage .


All of this was written down partly as a memo, and partly in order to save time for you (the task is unlikely to be solved too often), because if a stone flower does not come out, feel free to write, there is a chance that I forgot to describe some kind of detail, and if I didn’t forget, suddenly, than I will help ...


Also popular now: