1

I am trying to create my own verion of local-npm

I have this simple http server:

#!/usr/bin/env node
'use strict';

import http = require('http');

const s = http.createServer(function (clientRequest, clientResponse) {

  if (clientRequest.url === 'x') {
    clientResponse.write('retrieve the tarball from local fs');
    clientResponse.end();
    return;
  }

  const proxy = http.request({
      hostname: 'https://registry.npmjs.org',
      port: 80,
      path: clientRequest.url,
      method: clientRequest.method
    },
    function (res) {
      res.pipe(clientResponse);
    });

  clientRequest.pipe(proxy);

});

s.listen(3441);

In a local terminal, I run:

npm config set registry "localhost:3441"

and just for kicks I run this too:

npm set registry "localhost:3441"

and to confirm it worked, I do:

$(npm get registry) => "localhost:3441"

but then when I run npm install, nothing gets intercepted by the proxy, everything just goes to NPM.

What am I doing wrong?

1 Answers1

1

So setting the registry is a bit different to a proxy.

Setting the registry will request a package from the set registry; if it is set to the default, npm registry will receive a request, however if something else is set, that will receive request.

Alternatively, setting the proxy will allow the set registry to be accessed via a certain domain. This is what I have to set at work for npm to work.

npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080

ref


Setting the registry is pretty much the same.

npm config set registry http://localhost:3441
  • 1
    thanks, yeah with the code in the OP, `npm install x` would hit the registry server, but a plain `npm install` would not ... not exactly sure why –  May 15 '18 at 02:55
  • Did you try removing all `node_modules` first? –  May 15 '18 at 02:58
  • Likely, have you checked this out; [local-npm](https://github.com/local-npm/local-npm)? it seems to do what you seem to be after –  May 15 '18 at 03:25
  • naw, `local-npm` can't do what I want, that's why I am trying to do something a little smaller in scope –  May 15 '18 at 03:25
  • So your idea worked, I deleted `node_modules`, but now I get this error: `Only HTTP(S) protocols are supported` –  May 15 '18 at 03:26
  • NPM requires SSL by default. You won't have an ssl cert by default, so make sure you set ssl off (not recommended in a normal config) `npm config set strict-ssl false` –  May 15 '18 at 03:29
  • yeah actually I think that error was because this is bad `npm config set registry localhost:3441` and this is good: `npm config set registry http://localhost:3441` –  May 15 '18 at 03:40
  • thanks man this really helped, I got it working, one thing that's kinda funny though, is why I don't need to manually include headers to the proxy request, see this question: –  May 15 '18 at 03:56
  • https://stackoverflow.com/questions/50342072/why-doesnt-node-js-proxy-request-need-headers-included –  May 15 '18 at 03:56