Friday, October 28, 2016

Offline use of NPM

Jaisalmer, Rajasthan
A somewhat technical post for those of you who are using NPM:

A problem I get a lot when traveling is the use of NPM for downloading packages to build my projects. Now, for new packages I never used I can understand that there's a problem because I don't expect the entire NPM repository to be on my computer.However I often use bootstrap for different projects and I don't want to wait for it to download each time. What's more, I can't download it without an internet connection and thus I'm missing work time because I miss bootstrap.

This is what I tried:

1.      Hacking a bit:

Go to a repository that already has bootstrap and copy the folder from node_modules folder. You need to remember also to copy the line stating the dependence from that repository package.json file.
It works really good when I try to do it with simple packages like bootstrap. But what happens if I want to do it for React or Webpack? They rely on other packages, and I need to know which ones. These can be 100s of packages, it's a lot of work to start a new project.
I can just have a base repository that I duplicate every time, but what's the fun of this?

2.      Using "NPM –cache-min 99999999 install <package>"

This would force NPM to load from the cache. A few problems:
1.       NPM will still connect to the registry if there's a package with dependencies that are not in the cache.
2.       It will fail in case of downloading a package which has dependencies on a newer version than one in the cache.

3.      Local-NPM

I found a program called local-NPM (https://github.com/nolanlawson/local-NPM), you're more than invited to use it on your projects. It's unmaintained, but it seems like it's working.
Its use includes running a local proxy server, and NPM downloads the packages from it instead of downloading it from the NPM registry. If the package you're looking for is not available in the proxy it will download it from the NPM registry and save it there.

You can use the instructions on the GitHub page or you can read this blogpost https://addyosmani.com/blog/using-NPM-offline/ by a guy named Addy Osmani. It's his first post I read, but many thanks to him for writing of this solution. Note what it says on GitHub regarding to the use of a temporary .npmrc file.

4.      Yarn and big-bertha

The same day I started writing this post, a guy in my FCC cohort group (FCC Rhinos! :)) told us about a new package manager developed by Facebook. It's called Yarn and it should fix some problems they had with NPM. It fixes some security issues, and should also be faster. It does work with the NPM registry, not using a new one, so any package installable with NPM is also installable with Yarn. It also supports offline installation. I still didn't manage to use it very well. Fromwhat I understand, my internet connection is too slow and thus Yarn hangs up in the middle of the installation.

An NPM package "big-bertha" (https://github.com/joshmatz/big-bertha) comes along just to add commonly used packages to your cache so you can install them offline. You just run "yarn add big-bertha && yarn remove big-bertha" in your cmd and you're good to go. For some reason even though I did run it before successfully (with an internet connection), I can't install it again offline. I will need to check it and I'll update you.

It seems like the same solution as npm –cache-min, and it's probably suffering from the same problems. I'll need to dig into these solutions more.

Ok, so I didn't find a very good solution, I will continue to look for one and update you when I find or if one of the former solutions does work eventually.

Do you know of anything else I can do? Please tell me about it in the comments or on my blog.

No comments:

Post a Comment