Multiple NPM Registries When One is Behind VPN - node.js

At work we have an internal NPM Registry that houses our internal modules and passes through to npmjs.org for modules that are not in the registry. In order to use it I've added the following line to my ~/.npmrc file
registry=http://**privateurl**
Which works great while I'm at work or connected to the VPN but when I'm working on other stuff at home and not on the VPN running npm install fails because the private repository cannot be reached.
Is there a way I can add a timeout and a fallback to npmjs.org if the private npm repository cannot be found?

You could run npm install with --reg option from home forcing to fetch from npmjs.org
npm install express --reg https://registry.npmjs.org
... as mentioned in this SO Answer.

Related

Node-gyp fetches headers from internet – not allowed because of firewall rules

I am trying to setup a simple node app in docker. The app needs to install npm package ibm_db. In that installation of ibm_db, node-gyp wants to download headers file from the internet which is not allowed under company firewall rule. Downloading npm packages works fine using a proxy.
https://nodejs.org/download/release/v16.14.2/node-v16.14.2-headers.tar.gz failed, reason: unable to get local issuer certificate
I am unsure of the best way forward here. Is it possible to install the headers.tar manually somehow?
Commit the specific version of the node-{NODE_VERSION}-headers.tar.gz file that you require into the repo, and do this:
echo "---> Set tarball"
NODE_VER=v16.13.1
npm config set tarball /{YOUR_PATH}/node-${NODE_VER}-headers.tar.gz
Then proceed with your npm install as you would normally.
https://github.com/nodejs/help/issues/3686#issuecomment-1011865975

What are exact destinations in internet that are needed to be opened in order for `npm install` command to be working?

I have searches for internet, but I am not sure:
I have installed NodeJS on my machine inside private network. I need npm install command to be working on private network. Security teem is asking me exact destinations in internet that are needed to be opened in order for npm install command to be working.
What are these destinations?
P.S. Ideally I would like all npm commands to be working. What destinations are needed in this case?
Thank you
https://registry.npmjs.org, this is the default registry for all the npm packages but you can change it by configuring ".npmrc" file.
NPM (Node Package Manager) uses by default the public repository https://registry.npmjs.org/ so if you will use public packages as dependencies that's the domain from where it resolves the dependencies to download them. Here is the documentation about NPM: https://docs.npmjs.com/cli/v8/using-npm/registry
Although, your projects could require private packages as dependencies, and those could be stored in private repositories (GPM, Artifactory, etc.). In that scenario you will need to know from where your project is downloading those dependencies.
There are two places where you can see the registry used in your case:
.npmrc file located at you user directory with the global configuration.
.npmrc file located at the root of your project managed by NPM.
// .npmrc
registry=https://registry.npmjs.org/

How to change package source for NPM

We are developing in a secured environment that has no connection to the internet.
For nuget, we have our own package sources, directories on our secured network and we copy the packages we need into it (after screening).
For npm we want something similar. How do I configure where npm gets its packages?
you should set the registry using: npm config set registry
see npm registry

How can I install the latest version of Typescript without Internet (offline)?

I have Internet connection in my home and I can install the latest version of TypeScript with this command: npm install -g typescript , But unfortunately There is no Internet at my work place (in fact we are not allowed to use Internet).
Beside this I googled But It seems There is no offline installer for Typescript. My question is how can I handle this problem ?
I am totally new to npm and a step by step workaround would be appreciated .
There is an ugly solution: do npm install at home and copy the content of your globally installed packages folder to work.
If you want to be able to do npm install without access to the internet you will need to configure your own npm registry in your local network.
I've used Sinopia in the past when working offline. It works as a cache for npm allowing you to work off-line provided you have installed the required packages while having an internet connection.
As per https://www.npmjs.com/package/sinopia#installation you can install and configure Sinopia with the following steps:
# installation and starting (application will create default
# config in config.yaml you can edit later)
$ npm install -g sinopia
$ sinopia
# npm configuration
$ npm set registry http://localhost:4873/
# if you use HTTPS, add an appropriate CA information
# ("null" means get CA list from OS)
$ npm set ca null

Install NPM packages without Internet

I'm very new to npm, and I need to work with NPM packages like express, express-generator, ejs, mysql, etc on a server with no Internet access. This means that simply using npm install express will not work since I won't be able to connect to the NPM registry.
Do I go to the GitHub pages of each of the packages and download the zip files (e.g. https://github.com/strongloop/express/archive/master.zip), then do a npm install ./master.zip?
What I'm worried is that each of these packages in turn require a ton of other dependencies, which I have to then download individually.
One possible solution is setting up your own private NPM registry. Some of the advantages are:
NPM will work as it meant to
You will have a central place inside your company that can serve other developers/CI servers
It can be used to deploy your private NPM packages
Governance and security
You will need to deploy the packages you require into the private registry, or if possible have it proxy the public NPM registry.
There are multiple solutions available for setting up a private registry. For example you can use the npm-registry-couchapp or a Binary repository manager which supports NPM such as Artifactory (disclaimer - I'm affiliated).

Resources